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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

SS file IF syntax


Go to End


16 Posts   8683 Views

Avatar
BlueScreen

Community Member, 36 Posts

24 March 2011 at 12:41pm

Edited: 24/03/2011 12:43pm

Hello, I'm looking for more documentation on the full capabilities of IF statements within SS files.

Information on cool little tricks like <% control %> and <% if %> seems to be sparse and it would be great if I knew what else you could do.

Specifically IF statements because want to use them to make certain parts of the SS conditional depending on values it gets from the controller.

EG:

Say I have the following SS code

<table>
                  <tr>
                    <td>$variable1</td>
                  </tr>
                  <tr>
                    <td>$variable2</td>
                  </tr>
</table>

I want to make it so that if $variable2 is just blank then the entire row it sits on disappears

<table>
     <tr>
       <td>$variable1</td>
     </tr>
      <% if $variable2=="" %>
         <tr>
           <td>$variable2</td>
         </tr>
      <% end_if %>
</table>

Or maybe...

<table>
     <tr>
       <td>$variable1</td>
     </tr>
      <% if checkNull($variable2) %> <!--pass variable to controller->
         <tr>
           <td>$variable2</td>
         </tr>
      <% end_if %>
</table>

...Or something like that. I can't find any guides or tutorials out there that specify the exact syntax of these statements so I don't know the proper way to go about this

Avatar
Willr

Forum Moderator, 5523 Posts

24 March 2011 at 8:39pm

Have you seen this page - http://doc.silverstripe.org/sapphire/en/reference/built-in-page-controls. That should have all the examples you'll need to SS 2.*. In 3.0 this will be changed quite heavily.

For your direct questions you would use

1)

<% if variable2 %><% else %>... (cannot use quotes in the 2.* parser)

2)
You cannot pass a variable into a if or control for 2.*. This is since the template parse is not a proper grammer parser, simply does several find and replace commands and it cannot expand variables within those. I believe you can do this in the new parser for 3.0.

Avatar
BlueScreen

Community Member, 36 Posts

25 March 2011 at 9:22am

Ok, that helps. Though, going back to this example:

<table>
<tr>
<td>$variable1</td>
</tr>
<% if $varaible2 %>
<tr>
<td>$variable2</td>
</tr>
<% end_if %>
</table>

Are you suggesting that if $variable2 is a blank string then silverstripe will equate it to FALSE inside the conditional statement?

Or do you mean that $variable2 must be a boolean value which I would then define with code like this?

function checkNull() { if (empty($this->variable2)) { return false; } }

Avatar
Willr

Forum Moderator, 5523 Posts

25 March 2011 at 11:29am

Are you suggesting that if $variable2 is a blank string then silverstripe will equate it to FALSE inside the conditional statement?

I believe this is the behaviour for text fields. You can see what <% if %> will evaluate in the field types hasValue() function.

<% if $varaible2 %>

FYI - You don't need to put the $ when you're inside statements (if, control).

Avatar
BlueScreen

Community Member, 36 Posts

28 March 2011 at 2:29pm

Edited: 28/03/2011 2:32pm

Willr, I am not sure we are on the same page. $variable2 is a PHP variable that is passed to the SS via the page controller. I do not know why you mention text fields as I am not using forms in this example.

So far,this:

<table>
<tr>
<td>$variable1</td>
</tr>
<% if variable2 %>
<tr>
<td>$variable2</td>
</tr>
<% end_if %>
</table>

Just gives me an error.

I am under the impression that what I am doing is simply not possible as the SS code has no ability to manipulate the PHP variables passed to it by the controller. It can only display them.

The closest thing I have been able to do is this:

<table>
<tr>
<td>$variable1</td>
</tr>
<% if function %>
<tr>
<td>$variable2</td>
</tr>
<% end_if %>
</table>

where 'function' refers to a PHP function in the page controller that returns 'true' or ''false' depending on the content of $variable2.

Avatar
Willr

Forum Moderator, 5523 Posts

28 March 2011 at 2:38pm

I mentioned text field as all template variables are usually database field types (http://api.silverstripe.org/2.4/sapphire/model/DBField.html). Each var is normally wrapped in those so that the template engine knows if it needs to escape them or format it in a specific way. If you are passing a variable to your template you should wrap it in the DBField which makes sense for your data type i.e

function Hello () {
return DBField::create('Text', "Hi Will!");
}

$Hello <!-- returns Hi Will -->

Avatar
BlueScreen

Community Member, 36 Posts

28 March 2011 at 10:43pm

Edited: 28/03/2011 10:49pm

I see, you are referring to the method of declaring the variables?

I use something like this:

public function data() {

$data = new dataObjectSet();

foreach(Value in my Array) {
  $row = array(
    'variable1' => 'text from array',
    'variable2' => 'text from array and the occasional NULL',
  ));

  $data = push(new arrayData($row));
}

return $data;

}

Note: what I just wrote above is just from the top of my head, not my actual code so it probably is not accurate but assume my code doesn't have any blatant run-time errors in it

So if I want to make a table out of that which dynamically adjusts its number of rows per item, I just have

  <table>
<% control data %>
    <tr>
      <td>$variable1</td>
    </tr>
    <tr>
      <td>$variable2</td>
    </tr>
<% end_control %>
  </table>

The problems comes when those variables might contain just "" instead of any actual content. I want to make it so that if the variable is just blank, the code does not proceed with rendering the entire table. So I need to wrap the table in an IF

  <table>
<% control data %>

    <% if variable1 not null %>
      <tr>
        <td>$variable1</td>
      </tr>
    <% end_if %>

    <% if variable2 not null %>
      <tr>
        <td>$variable2</td>
      </tr>
     <% end_if %>
<% end_control %>
  </table>

The question is, can that if statement analyze the content of the variables directly?

Avatar
Willr

Forum Moderator, 5523 Posts

28 March 2011 at 11:02pm

I see, you are referring to the method of declaring the variables?

Your example should work, you have it wrapped in an arraydata object which is good. If you test it by making 'variable2' => false does <% if variable2 %> then not work. Could be an issue with empty strings / null which you may need to handle in the PHP function.

Go to Top