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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Little Help with if statements


Go to End


4 Posts   1514 Views

Avatar
Yellow7 Jon

Community Member, 39 Posts

24 June 2008 at 4:54am

Hello All!

Im trying to do something like this

<% if TotalItems - Pos = 6 %>
... do this code ...
<% end_if %>

but keep getting an error. is there a way to do PHP style if statements in a template?

as in <= , >= , % , ect.

Avatar
Double-A-Ron

Community Member, 607 Posts

24 June 2008 at 8:48am

What you should do is in the Page_Controller class for that page type, create a new function that does the calculation and returns a boolean to your template. Something like:

function FunctionName() {
   if($this->TotalItems - $this->Pos == 6) {
      return true;
   } else {
      return false;
   }
}

Note, I am unsure what TotalItems and Pos actually are, but I am assuming they are part of the page object if you are referencing them in the template.

So, then, in your template:

<% if FunctionName %>
... do this code ...
<% end_if %> 

The idea is to remove such logic from your template and always handle it in your controller.

Cheers
Aaron

Avatar
Yellow7 Jon

Community Member, 39 Posts

24 June 2008 at 9:00am

Ahh well that makes sense! Thanks for the heads up!!

Avatar
Double-A-Ron

Community Member, 607 Posts

24 June 2008 at 9:14am

Oh and to answer your question, no, you can't use PHP in a template. This forces you to use the MVC model the way you should.

Cheers
Aaron