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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

include a custom javascript file


Go to End


7 Posts   7289 Views

Avatar
johannes

Community Member, 20 Posts

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

Forum Moderator, 4102 Posts

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.

Avatar
johannes

Community Member, 20 Posts

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

Avatar
Hamish

Community Member, 712 Posts

21 January 2009 at 7:02am

Edited: 21/01/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.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

21 January 2009 at 8:47am

::applause::

Avatar
johannes

Community Member, 20 Posts

23 January 2009 at 8:49pm

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

Avatar
Markiv

Community Member, 2 Posts

21 April 2012 at 6:44pm

Edited: 21/04/2012 6:44pm

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