DAS
DAS

Reputation: 1941

SilverStripe: Loop If Statement

I'm building a single page portfolio with SilverStripe.

So far I am able to loop through my sites and all $Title[s] and $Content[s] are visible in one <div>. Now I want to attach a certain CSS-class if the current page (which is looped) is named "Contact".

Something like:

//Pseudocode:
<loop start>
if ($Title == 'Contact') <div class="a"></div> else <div class="b"></div>
<loop end>

Does anybody know how to do this?

Upvotes: 1

Views: 1421

Answers (3)

Gavin Bruce
Gavin Bruce

Reputation: 1809

you could try a custom getter method such as :

function DivClassName() {
  return $this->Title == 'Contact'?'a':'b';
}

and use the following in your template.

<div class="$DivClassName"></div>

keeps the logic out of your templates :)

Upvotes: 1

DAS
DAS

Reputation: 1941

<% if $Title == "Contact" %>
     // Do something ...
<% end_if %>

Upvotes: 3

Fraser
Fraser

Reputation: 14244

<% if Title = "Contact" %>
    <div class="a"></div>
<% else %>
    <div class="b"></div>
<% end_if %>

Upvotes: 0

Related Questions