Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » string length after trim in conditional

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

Page: 1 2
Go to End
Author Topic: 3241 Views
  • MarcelKlomp
    Avatar
    Community Member
    5 Posts

    string length after trim in conditional Link to this post

    When using <% if SomeField %> conditional fields with only spaces in them are displayed as well. Is there a way to do something like if(trim(strlen($var))>0) in template conditionals?

    I'm aware I could do this with a custom function in my php file but then I'd have to check all fields individually which seems rather elaborate.

    My apologies in advance by the way if this is something documented or already previously answered.

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: string length after trim in conditional Link to this post

    Is there a way to do something like if(trim(strlen($var))>0) in template conditionals?

    No, you will need a method in your controller to do this logic. You could have a function called StringExists($string) and then you have a reusable <% if StringExists(FieldName) %>

  • MarcelKlomp
    Avatar
    Community Member
    5 Posts

    Re: string length after trim in conditional Link to this post

    Thanks Willr, sweet and simple! Why didn't I think of that?

  • spankmaster79
    Avatar
    Community Member
    46 Posts

    Re: string length after trim in conditional Link to this post

    hi,

    could you please tell me why this doesn't work for me? The field in the database is Varchar field. And my function looks like this:

    /**
        * check if a string exists
        * @param unknown_type $varname
        */
       public function StringExists($string) {
          if(trim(strlen($string))>0) {
             return true;
          }
          return false;
       }

    I don't even see the function getting called. In Page.ss I have:

    <% if StringExists(URL) %>
    <a href="$URL" class="ext" >$Titel</a>
    <% end_if %>

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: string length after trim in conditional Link to this post

    You can't pass variables in the template, only literal strings. If you want to check it on a property of the current object, then your controller method should be like this:

    public function StringExists() {
    if(trim(strlen($this->URL))>0) {
    return true;
    }
    return false;
    }

    As a side note, i'm slightly baffled as to why anyone would trim() and strlen() as strlen() will return an integer :S

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: string length after trim in conditional Link to this post

    @Pigeon - to make sure if someone put a single space that the string is counted as blank?

  • martimiz
    Avatar
    Forum Moderator
    1085 Posts

    Re: string length after trim in conditional Link to this post

    if(strlen(trim($this->URL))>0) {...

  • MarcelKlomp
    Avatar
    Community Member
    5 Posts

    Re: string length after trim in conditional Link to this post

    @Pigeon yes, you're right of course. @swaiba answers the why, and @martimiz answers the how. My bad for putting it the wrong way around initially. Trust me however that in the actual code I have it in the right order.

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