3212 Posts in 847 Topics by 809 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 5208 Views |
-
include a custom javascript file

20 January 2009 at 11:02am
Hello, i try to build a demo page for css & javascript stuff.
Every page needs a custom css and a javascript file in its <head> - but its harder than i though.
The DemoPage Controller contains two file fields like this:
class DemoPage extends Page {
static $has_one = array(
'JS_file' => 'File' ,
'CSS_file' => 'File'
);function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new FileIFrameField('CSS_file'));
$fields->addFieldToTab('Root.Content.Main', new FileIFrameField('JS_file'));
[...]An here the template:
[...]
<head>
<% base_tag %>
<link rel="stylesheet" type="text/css" href="assets/demos/css/basic.css" />
<% if CSS_file %>
<link rel="stylesheet" type="text/css" href="$CSS_file" />
<% end_if %>
<% if JS_file %>
<script type="text/javascript" src="$JS_file"></script>
<% end_if %>
[...]When i attach a file and open the page i get the following error:
FATAL ERROR: Object::__call() Method 'forTemplate' not found in class 'File'
At line 199 in /opt/lampp/htdocs/port/sapphire/core/Object.phpuser_error(Object::__call() Method 'forTemplate' not found in class 'File',256)
line 199 of Object.phpand so on....hm i don't get it! tips would be great ^^
-
Re: include a custom javascript file

20 January 2009 at 11:23am
The File is an object, and you're trying to ouput that as HTML. Think about it -- how is a File object supposed to know what you want? Its Title? Filename? Creation date? ID?
Could be a number of things.
So, to solve your problem, you need this: $JS_File.URL
But, that said, you should really be using the Requirements class. That's what it's there for. Scripts don't belong in the head.
-
Re: include a custom javascript file

20 January 2009 at 11:00pm
Thank you for the answer - URL is what i was looking for. Haven't used SilverStripe for a while...
-
Re: include a custom javascript file

21 January 2009 at 7:02am Last edited: 21 January 2009 7:03am
Rather than using:
<% if JS_file %>
<script type="text/javascript" src="$JS_file"></script>
<% end_if %>in your template, add this to your controller:
class DemoPage_Controller extends Page_Controller {
function init() {
parent::init();
if($this->JS_file)
Requirements::javascript($this->JS_file()->URL);
}
}
Why?- Requirements will look after the correct placement (ie, at the end of the output),
- You have more control over it's generation (eg subclasses can block it, or overrides will be handled)
- You will have access to scripts already loaded through Requirements (eg, prototype, if it has been requested)
- Requirements will combine JS files and increase speed if it can
For example, lets say page loads jQuery.js, then you add a piece of jQuery script in the head, you will get an error like '$' is not defined, because jQuery will be added at the end of the page.
- Requirements will look after the correct placement (ie, at the end of the output),
-
Re: include a custom javascript file

23 January 2009 at 8:49pm
Thanks a bunch, I will have a look at the requirements class.
-
Re: include a custom javascript file

21 April 2012 at 6:44pm Last edited: 21 April 2012 6:44pm
how to include cufon for site.Where i have to place my cufon.js file ..???.
| 5208 Views | ||
|
Page:
1
|
Go to Top |


