Jump to:

23023 Posts in 11882 Topics by 2830 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: 3963 Views
  • BlueScreen
    Avatar
    Community Member
    36 Posts

    SS file IF syntax Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: SS file IF syntax Link to this post

    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.

  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: SS file IF syntax Link to this post

    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; } }

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: SS file IF syntax Link to this post

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

  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: SS file IF syntax Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: SS file IF syntax Link to this post

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

  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: SS file IF syntax Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: SS file IF syntax Link to this post

    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.

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