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.

Archive

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

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

take data from function in template


Reply

7 Posts   3254 Views

Avatar
bebabeba

5 December 2008 at 11:41pm Community Member, 192 Posts

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?

Avatar
schellmax

6 December 2008 at 1:01am Community Member, 126 Posts

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'

Avatar
bebabeba

6 December 2008 at 2:42am Community Member, 192 Posts

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);
}

Avatar
schellmax

9 December 2008 at 7:01am Community Member, 126 Posts

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>
<?
}
?>

Avatar
Hamish

9 December 2008 at 11:18am Community Member, 712 Posts

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 %>

Avatar
bebabeba

9 December 2008 at 11:13pm Community Member, 192 Posts

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.

Avatar
Hamish

11 December 2008 at 7:46am Community Member, 712 Posts

Have a look at:

http://doc.silverstripe.com/doku.php?id=permission