Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

include a custom javascript file


Reply

7 Posts   5887 Views

Avatar
johannes

20 January 2009 at 11:02am Community Member, 20 Posts

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.php

user_error(Object::__call() Method 'forTemplate' not found in class 'File',256)
line 199 of Object.php

and so on....hm i don't get it! tips would be great ^^

Avatar
UncleCheese

20 January 2009 at 11:23am 4085 Posts

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.

Avatar
johannes

20 January 2009 at 11:00pm Community Member, 20 Posts

Thank you for the answer - URL is what i was looking for. Haven't used SilverStripe for a while...

Avatar
Hamish

21 January 2009 at 7:02am (Last edited: 21 January 2009 7:03am), Community Member, 712 Posts

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.

Avatar
UncleCheese

21 January 2009 at 8:47am 4085 Posts

::applause::

Avatar
johannes

23 January 2009 at 8:49pm Community Member, 20 Posts

Thanks a bunch, I will have a look at the requirements class.

Avatar
Markiv

21 April 2012 at 6:44pm (Last edited: 21 April 2012 6:44pm), Community Member, 2 Posts

how to include cufon for site.Where i have to place my cufon.js file ..???.