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   3267 Views

Avatar
bebabeba

Community Member, 192 Posts

5 December 2008 at 11:41pm

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

Community Member, 126 Posts

6 December 2008 at 1:01am

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

Community Member, 192 Posts

6 December 2008 at 2:42am

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

Community Member, 126 Posts

9 December 2008 at 7:01am

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

Community Member, 712 Posts

9 December 2008 at 11:18am

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

Community Member, 192 Posts

9 December 2008 at 11:13pm

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

Community Member, 712 Posts

11 December 2008 at 7:46am

Have a look at:

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