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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

Using Javascript to insert include

Go to End

2 Posts   1443 Views


Community Member, 5 Posts

4 September 2010 at 3:28am


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

For example:

<script type="text/javascript">

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


or something similar using document.write. Any ideas?



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) {
  alert('Load was performed.');

Hope thats enough to get you started!