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

Using Javascript to insert include


Go to End


2 Posts   1638 Views

Avatar
Nev

Community Member, 5 Posts

4 September 2010 at 3:28am

Hi,

I'm have an issue where I want to use the <% include somepage.ss %> within a Javascript if else statement but its not working.

For example:

<script type="text/javascript">

if(loggedin==true){
<% include LoggedIn %>
}else if(loggedin==false){
<% include LoggedIn %>
}

</script>

or something similar using document.write. Any ideas?

Thanks!

Avatar
Willr

Forum Moderator, 5523 Posts

4 September 2010 at 7:52pm

I think you will have to use something like ajax to get the rendered template rather than simply including it as it won't parse the file at all. You could do something like create a function on a controller which returns the renderWith() then get that via ajax

// in Page_Controller 

function LoggedInMenu() {
return $this->renderWith('LoggedInMenu');
}

If you're using jQuery then you can use the .get() function to call that method and append it to the template

// new javascript file...

(function() {
$(document).ready(function() {

$.get('home/LoggedInMenu', function(data) {
  $('#menu').html(data);
  alert('Load was performed.');
});
});
})(jQuery);

Hope thats enough to get you started!