Jump to:

3443 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » unable to call Inline Javascript in Template file

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

Page: 1
Go to End
Author Topic: 2210 Views
  • ramu
    Avatar
    Community Member
    15 Posts

    unable to call Inline Javascript in Template file Link to this post

    Hi ,

    I am unable to call the javascript script in template file while using ajax functionality. please find the code below

    DemoPage.php

       function DemoContent()
       {
          /*$records = DataObject::get( "ProductMainPage", "", "", "", "");*/
          $records = DataObject::get( "DemoPage", "ParentID = $this->ID", "", "", "");
          return $this->customise(array(
             'DemoMainPage' => $records
          ))->renderWith('DemoContent');
       }

    DemoContent.ss

    <script>

    function samplefunction()
    {
    ........
    }

    </script>

    <div>

    [demo value]

    </div>

    http://localhost/myproject/demo-page/DemoContent - unable call the javascript inside the render template file. please do needful . let me know if you need any clarification.

    Regards,
    thisIsRam

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: unable to call Inline Javascript in Template file Link to this post

    I solved this by creating a seperate template file and render it as javascript like this:

    class MyPage_Controller extends Page_Controller {

    function googlemapjs(){
          SSViewer::set_source_file_comments(false);
          ContentNegotiator::disable();
          $this->getResponse()->addHeader('Content-Type', 'text/javascript; charset="utf-8"');
          return $this->renderWith("GoogleMapJS");
       }

    I created a file in the templates folder called GoogleMapJS.ss with the javascript code and call it from another template with:

    <script src="$URLSegment/googlemapjs" type="text/javascript"></script>   

    In this way I could use a dynamic variable to call XML file in the same way with data from an Object that contains the Address data.

    Hope it helps.

  • ramu
    Avatar
    Community Member
    15 Posts

    Re: unable to call Inline Javascript in Template file Link to this post

    Hi martiji,

    Thanks for your reply,

    I got some answer from you. it will use for dynamic script call from globally and act like separate script file.

    but still i get suffer to call the script in template file. because while calling Ajax script ,

    any possible to call inline JavaScript in render template file or to combine both JavaScript and normal ss template in one render file

    else any other possible way to clear the issue. For example

    <script src="$URLSegment/googlemapjs" type="text/JavaScript"></script>

    DemoPage.php

    function DemoContent()
    {
    /*$records = DataObject::get( "ProductMainPage", "", "", "", "");*/
    $records = DataObject::get( "DemoPage", "ParentID = $this->ID", "", "", "");
    return $this->customise(array(
    'DemoMainPage' => $records
    ))->renderWith('DemoContent');
    }

    DemoContent.ss

    <script src="$URLSegment/googlemapjs" type="text/javascript"></script>
    <div>
    [demo value]
    </div>

    Regards,
    thisIsRam
    Ahsan Technologies (Pvt) Ltd,
    www.agriya.com,www.anova.tv,www.isocial.in,www.markit.me

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