Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

string length after trim in conditional


Go to End
Reply


14 Posts   3437 Views

Avatar
martimiz

Forum Moderator, 1132 Posts

17 January 2011 at 1:00am

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

Avatar
timwjohn

Community Member, 98 Posts

14 June 2011 at 3:11am

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

Avatar
timwjohn

Community Member, 98 Posts

16 June 2011 at 2:30am

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?

Avatar
Willr

Forum Moderator, 5513 Posts

16 June 2011 at 4:01pm

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

Avatar
timwjohn

Community Member, 98 Posts

17 June 2011 at 1:46am

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!

Avatar
martimiz

Forum Moderator, 1132 Posts

17 June 2011 at 2:52am

Edited: 17/06/2011 2:58am

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

Go to Top