Jump to:

3434 Posts in 1026 Topics by 866 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: 3191 Views
  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

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

    O... and I seem to recall that you cannot use variables as functionparams in a control structure. So maybe something like this would do the trick:

    /**
    * check if a string exists
    * @param unknown_type $varname
    */
    public function URL() {
    return trim($this->URL);
    }

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

  • timwjohn
    Avatar
    Community Member
    98 Posts

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

    As willr suggested though, you can still use the field name. Then you have a reusable method in your Page class that can be utilised throughout your site, and you won't need to create a separate method for every field you want to test. This is what I did:

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

       <% if FieldIsPopulated(Commendations) %>
          <h2>Commendations</h2>
          $Commendations
       <% end_if %>

       <% if FieldIsPopulated(Notes) %>
          <h2>Notes</h2>
          $Notes
       <% end_if %>

  • timwjohn
    Avatar
    Community Member
    98 Posts

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

    Willr, I just realised that the function as you suggested works great in the page that you are viewing, but not in a control loop when referring to a property of an iterated item. Any idea how to make this work?

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

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

    Then you'll need put the method on the model instance and not the controller.

  • timwjohn
    Avatar
    Community Member
    98 Posts

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

    Thanks Willr, that works throughout now!

    Can anybody explain exactly why it works universally in the model, but not in the controller. Or at least point me in the direction of a document than can help explain. It would benefit me greatly in my understanding of SilverStripe's insides!

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

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

    I remember finding this confusing at first, but I guess it isn't really. I just hope I can explain it clearly This is how I understand it:

    Each template is 'managed' by a controller. Every property/method-call from within a template is answered by the boss controller. Within each template we can use <% control %> loops to iterate DataObjectSets, that consist of DataObjects.

    Applying this to pages: each page type consists of two classes: a data model (the Page class, an extended DataObject) and a controller (the Page_Controller class). The controller controls the Page template. Fortunately these page controllers have built-in access to the data within their Page Datamodel - including your custom methods it seems .

    Controller methods like Menu() and Children() however return DataObjectSets of pages that can then be looped in a control structure - and that's just it: they are Page DataObjects and not Controllers. So within a control loop you have access to the Page class itself meaning methods defined the controller are not available. Same goes for custom methods that return things like DataObject::get('SiteTree',..) results. Remedy this by adding appropriate methods to the data model (Page) class...

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