21303 Posts in 5736 Topics by 2603 members
| Go to End | ||
| Author | Topic: | 3036 Views |
-
Re: SS file IF syntax

29 March 2011 at 10:16am Last edited: 29 March 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
-
Re: SS file IF syntax

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.
-
Re: SS file IF syntax

30 March 2011 at 10:18am Last edited: 30 March 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?
-
Re: SS file IF syntax

30 March 2011 at 6:03pm Last edited: 30 March 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.
-
Re: SS file IF syntax

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?
-
Re: SS file IF syntax

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?
-
Re: SS file IF syntax

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 %>.
-
Re: SS file IF syntax

7 April 2011 at 10:00am Last edited: 7 April 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.
| 3036 Views | ||
| Go to Top |

