Jump to:

3392 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » include a custom javascript file

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

Page: 1
Go to End
Author Topic: 5682 Views
  • johannes
    Avatar
    Community Member
    20 Posts

    include a custom javascript file Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: include a custom javascript file Link to this post

    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.

  • johannes
    Avatar
    Community Member
    20 Posts

    Re: include a custom javascript file Link to this post

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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: include a custom javascript file Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: include a custom javascript file Link to this post

    ::applause::

  • johannes
    Avatar
    Community Member
    20 Posts

    Re: include a custom javascript file Link to this post

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

  • Markiv
    Avatar
    Community Member
    2 Posts

    Re: include a custom javascript file Link to this post

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

    5682 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.