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

unable to call Inline Javascript in Template file


Go to End


3 Posts   3368 Views

Avatar
ramu

Community Member, 15 Posts

26 November 2009 at 7:37pm

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

Avatar
Martijn

Community Member, 271 Posts

27 November 2009 at 2:11am

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.

Avatar
ramu

Community Member, 15 Posts

28 November 2009 at 12:08am

Edited: 28/11/2009 12:09am

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