0
votes

I'm struggling to get something that should be simple to work. I was trying the following in a silverstripe template file without success :

<% if $objectCount > 10 %>

I have read on the silverstripe another person having the same problem with no satisfying answer ( http://www.silverstripe.org/template-questions/show/10053 ) So I wanted to come up with a alternative in the meantime other than a basic method :

<% if $objectCountGreaterThanTen %>

So I've implemented a simple class hiding the conditional logic as follow :

class EvaluateLogic extends DataObject {
   public function greaterThan($exp, $x) { 
      print_r('calling logic.greaterThan()');
      return $exp >  $x;
   }
   public function lessThan($exp, $x) { return $exp <  $x; }
   ...
}

Then I instantiate an EvaluateLogic member ( named $logic ) in the DataObject class I use in the template by doing this :

<% if $logic.greaterThan($objectCount, 10) %>

but the .greaterThan() call is being ignored for some reasons.. Which I don't understand ? To partition the issue I have created a getLogic() accessor in my DataObject to also print some thing and see if the code was reached, which it is, but then the print_r in the greaterThan() function is not called.

Any ideas how to make this work ?

Thanks

4
Damn, I feel a little ashamed, the mistake was just under my nose. There is obviously no $ sign allowed within the <% if %> - DarkUrse

4 Answers

0
votes

As it turns out, even though removing the $ sign fixed my method call, it didn't allow me to achieve what I wanted. It seems that the ss parser isn't powerful enough to interprete method variables just yet : the 'objectCount' variable was interpreted as a string .

0
votes

You could try typecasting the variables. Since greaterThan and lessThan are using numbers, you can typecast with (int) or (float).

class EvaluateLogic extends DataObject {
   public function greaterThan($exp, $x) { 
      print_r('calling logic.greaterThan()');
      return (float)$exp > (float)$x;
   }
   public function lessThan($exp, $x) { return (float)$exp < (float)$x; }
   ...
}

http://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

0
votes

The template parser does not allow you to pass dynamic variables into method calls. This is by design - it stops model or controller logic being pushed into templates (where that logic really has no place)

You need to store the data on your model or controller and access it there as a property of the model or controller.

If you are looking to change a behaviour or output when a certain number of iterations have been processed, you can use $iteratorPos (some discussion here about doing that in your template loops: http://www.silverstripe.org/general-questions/show/16838)

0
votes

The easiest way, and the correct, way would be to return the objectCount to the template from the controller:

function templateName(){
    return array(
        "Objects"=>$objects,
        "AboveTen"=>$objects->count() > 10 ? true : false
    );
}

Furthermore, there are also two variables called $TotalItems and $Pos in any loop. The $TotalItems variable will give you the total amount of objects being looped through, while the $Pos will give you the current object out of the total, as in 5/10.

If you wanted the loop to stop at 10, you could also do (v3):

<% loop Object.Limit(10) %>

More information here http://doc.silverstripe.org/framework/en/reference/templates