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.

General Questions /

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

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

SS file IF syntax


Go to End
Reply


16 Posts   4485 Views

Avatar
BlueScreen

Community Member, 36 Posts

29 March 2011 at 10:16am

Edited: 29/03/2011 10:17am

That might work and I can check on that, but realistically I cannot really assign 'variable2' to false. Variable2 is supposed to contain information that is conveyed to the user

Be aware that my actual code is far, far more sophisticated than just displaying 'variable1 and 2'. It is designed to display table data of any shape and size from a database

Avatar
Willr

Forum Moderator, 5513 Posts

29 March 2011 at 11:54am

That might work and I can check on that, but realistically I cannot really assign 'variable2' to false. Variable2 is supposed to contain information that is conveyed to the user

I know, I was suggesting that purely to test your if method so you can see it working!

Of course you would have something more like 'variable2' => ($variable2) ? $variable2 : false in your final code but for the sake of this thread I was purely recommending you play around with values in there to see what happens / experiment.

Avatar
BlueScreen

Community Member, 36 Posts

30 March 2011 at 10:18am

Edited: 30/03/2011 10:20am

Oh...sorry. Well I assure you I have been experimenting with different combinations and the IF statements in general do work. I just need good logic to give them.

'variable2' => ($variable2) ? $variable2 : false

I have seen other people use syntax like this, though never quite learned how it works exactly. It looks like some kind of conditional statement. Can you elaborate on it?

Avatar
Willr

Forum Moderator, 5513 Posts

30 March 2011 at 6:03pm

Edited: 30/03/2011 6:05pm

It is known as a ternary operator. Basically a short hand ifthenelse statment... (http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary). You'll find it used throughout SilverStripe.

Avatar
BlueScreen

Community Member, 36 Posts

31 March 2011 at 11:32am

Turns out, a well placed ternary operator was the answer. The following appears to work:

public function data() {

$data = new dataObjectSet();

foreach(Value in my Array) {
$row = array(
'varaible1' => $StaticText,
'variable2' => ($TextOrNULL == "") ? false : $TextOrNULL, //<--if NULL instead of text, change to boolean 'false'
));

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

return $data;

}

Which works fine with what I already have:

<table>
<% control data %>
<tr>
<td>$variable1</td> <!--always present-->
</tr>
<% if variable2 %> <!--if false, don't show the table row, proceed if has content-->
<tr>
<td>$variable2</td>
</tr>
<% end_if %>
<% end_control %>
</table>

Funny, I thought non-boolean objects equate to false by default?

Avatar
BlueScreen

Community Member, 36 Posts

6 April 2011 at 11:07am

I'm reviving this thread to bring up another IF statement issue.

I have syntax that looks like this:

<% control Items %>

<% if itemcount==0 %>

<table>
<% control Top.DisplayItems %>
...table rows filled with variables supplied by the DisplayItems function
<% end_control %>
</table>

<% else %>
Sorry there are no items
<% end_if %>

<% end_control %>

'itemcount' is a variable that contains a number. It is supplied by the Items function. The idea is that if this number is 0, there shouldn't be any items to display so the whole table that displays items should not be shown.

According to what I see in the cache files, this looks like it should work but it doesn't. itemcount always equates to false

What am I doing wrong here?

Avatar
Willr

Forum Moderator, 5513 Posts

6 April 2011 at 2:52pm

I would imagine it's a classic php type issue, 0, false basically are the same. If count if just counting Items the SS supports <% if Items %>.

Avatar
BlueScreen

Community Member, 36 Posts

7 April 2011 at 10:00am

Edited: 07/04/2011 10:01am

Okay that makes sense.

Actually itemcount is representing the number of properties that an item has, not the actual number of items. Sorry, my example didn't make that clear.

I worked around this just by using another boolean variable instead of 'itemcount' which assigns itself to true or false depending on the value of itemcount back in the page controller.

Go to Top