Jump to:

11004 Posts in 2735 Topics by 1824 members

All other Modules

SilverStripe Forums » All other Modules » A module that adds a JS tag to all pages

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 454 Views
  • JussiL
    Community Member
    1 Post

    A module that adds a JS tag to all pages Link to this post


    I have a task to create a SilverStripe (2.4) module, that has a really simple goal: It should just add a "<script src='http://...'></script>" JS-tag to the end of all pages on a SilverStripe site. That's like (for example) Google Analytics -module does.

    However, the JS-tag (mentioned above) includes a custom code (in src-attribute), that the administrator should be able to change by using CMS. I'm quite new to SilverStripe (especially when creating modules), but I have planned to create the module's admin menu by creating a custom admin area by extending class LeftAndMain.

    Currently I have just made the following simple files:


    class MyAdminPanel extends LeftAndMain {

       static $url_segment = 'myadminpanel';
       static $menu_title = 'myAdminPanel';

       public function init() {


    <div id="MyAdminPanel_left" style="margin: 5px;">

       <br />
       <p>Insert your code here:</p>
       <input type="text" class="text" />
       <br />


    <div id="MyAdminPanel_right">

       <style> h2 { margin: 0; padding: 0; } </style>
       <h2>Welcome to my module!</h2>
       <p>Write your code to the text field on the left and click save. Then the code is added to the script tag on every page.</p>

    My problem is, how to continue and expand those files? How should I make a script that adds "<script src='http://... code here'></script>" -tag automatically to every page on the site? The code (to the tag) should be taken from the input-field on MyAdminPanel_left.ss. Basically this module should work like Google Analytics module (which also adds a script tag to the end of every page), BUT the administrator should be able to modify the code (in the <script>-tag) from CMS.

    Is there anywhere good tutorials how to build modules and custom admin areas?

  • Chris_Bryer
    Community Member
    35 Posts

    Re: A module that adds a JS tag to all pages Link to this post

    create a class that extends DataObjectDecorator,
    in that class, you can add

    public function contentcontrollerInit(){
    $script = "alert('hi there')";

    add the extension to SiteTree:

    Object::add_extension('SiteTree', 'MyCustomDataObjectDecorator');

    now every page will pull in the custom script on init.
    take a look at the DataObjectDecorator Documentation to see how to add extra statics (add more stuff to the $db array in your decorator). from here, you can add a textfield so users can type in their own information script into each page, then you can pull that content and stick it in the required custom script. or if you want one script used on every page like GA, you can centralize it in a customized siteconfig. search ssbits.com for customizing siteconfig to do this.

    hope it helps,

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.