Jump to:

3447 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Using Javascript to insert include

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

Page: 1
Go to End
Author Topic: 1155 Views
  • Nev
    Avatar
    Community Member
    5 Posts

    Using Javascript to insert include Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Using Javascript to insert include Link to this post

    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!

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