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.

Template Questions /

Using Javascript to insert include


Reply


2 Posts   1198 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, 5513 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!