Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » take data from function in template

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 3242 Views
  • bebabeba
    Avatar
    Community Member
    192 Posts

    take data from function in template Link to this post

    Hi!
    Silverstripe sintax is a bit complex for me.
    So my problem is take data return by function in my template.
    If my function return a boolen, and I call $function_name in my template everything is ok.
    But if I need a if structure link
    <% if function return true %>
    ...
    <% else %>

    how can pass this value in template?

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: take data from function in template Link to this post

    simply use

    <% if Property %>
    ... optional content ...
    <% else_if OtherProperty %>
    ... alternative content ...
    <% else %>
    ... alternative content ...
    <% end_if %>

    <% if Property == value %>
    <% else %>
    <% end_if %>

    see http://doc.silverstripe.com/doku.php?id=templates
    look for 'If blocks'

  • bebabeba
    Avatar
    Community Member
    192 Posts

    Re: take data from function in template Link to this post

    ok..I read that document. Now I write my function. If I write $Login it's ok but I can't write
    <% if $_SESSION['user']=1 %> in my template.
    this is my function:

    function Login() {      
           if((Session::get('registrato', false))and($_SESSION['user']=1))
           {   return $_SESSION['user'];   
           }
          
        // Create fields
        $fields1 = new FieldSet(
        new TextField(
        $name = "nome",
               $title = "Username:",
               $value = ""
        ),
        new PasswordField(
           $name = "password",
           $title = "Password:",
           $value = ""
           )
            );
       
    // Create actions
    $actions = new FieldSet(
    new FormAction('doLogin', 'LOGIN')
    );

    return new Form($this, 'Login', $fields1, $actions);
    }

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: take data from function in template Link to this post

    well, using '<% %>' is for control blocks in silverstripe.
    what you're trying to get is a variable from plain php, so this might read:

    <?
    if($_SESSION['user'] == 1) {
    ?>
    <p>hello user!</p>
    <?
    }
    ?>

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: take data from function in template Link to this post

    Weird that you're recreating the security system.

    Why not use the existing security functionality. Then you can use:

    <% if CurrentMember %>

    from within your templates.

    Then you can use the usual hooks:

    <% if CurrentMember %>
    Hello $CurrentMember.FirstName
    <% else %>
    You are not logged in
    <% end_if %>

  • bebabeba
    Avatar
    Community Member
    192 Posts

    Re: take data from function in template Link to this post

    I recreate login system because was impossible for me to use again login widget. I need a secondary login area, that open for me 2 new pages, hidden to normal user. Only the privileged user that log in can see that.

  • Hamish
    Avatar
    Community Member
    712 Posts
    3242 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.