Jump to:

23552 Posts in 19349 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » SS file IF syntax

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

Page: 1 2
Go to End
Author Topic: 4370 Views
  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: SS file IF syntax Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: SS file IF syntax Link to this post

    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.

  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: SS file IF syntax Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: SS file IF syntax Link to this post

    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.

  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: SS file IF syntax Link to this post

    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?

  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: SS file IF syntax Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: SS file IF syntax Link to this post

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

  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: SS file IF syntax Link to this post

    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.

    4370 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.