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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How do I code OR statement for Template use?


Reply


5 Posts   1452 Views

Avatar
dela11

Community Member, 1 Post

30 October 2009 at 10:45am

I am attemting to use different layouts of the same template for different pages. I want a have a 2 column layout for some pages and a 3 column layout for others. it is all designed in my css to handle this. However I cannot code it correctly.

In the templates.Page>SS file I have the following code
<% if InSection(home) %>
<div id="main-2col"> $Layout </div>

<% else %>
   <div id="main"> $Layout </div>
   <!-- Good spot for a search box -->
   <div id="rightbar">    </div>
<% end_if %>

What I would like is the following:
<% if InSection(home) %>
or 2nd page
or 3rd page
<div id="main-2col"> $Layout </div>

<% else %>
   <div id="main"> $Layout </div>
   <!-- Good spot for a search box -->
   <div id="rightbar">    </div>
<% end_if %>

How can I include other pages in the IF statement correctly. I've tried but only get errors. Thanks.

Avatar
Mo

Community Member, 510 Posts

30 October 2009 at 11:35am

To be honest, I just make a seperate class called HomePage and use it to extend Page. I can then create a seperate template called HomePage.ss and create homepage specific layout/styles in there.

I may be wrong, but I do not believe you can use an OR in the templateing language (more info on page controls are here: http://doc.silverstripe.org/doku.php?id=built-in-page-controls).

Another alternative would be to add a tickbox to your Page in the CMS (say called 'Use 3Col' as an example). Then, you can should be able to use something like:

<% if Use3Col %>
<div class="main-3-col"></div>
<% else %>
<div class="main-2-col"></div>
<% end_if %>

Avatar
JoshuaLewis

Community Member, 76 Posts

31 October 2009 at 11:24am

Edited: 31/10/2009 11:32am

Perhaps adding a method to the Page_Controller class of Page.php would do the trick.

Example:
public function colNumber()
{
if (/*condition 1 */ || /*condition 2*/ || /*condition 3*/){
return true;
} else {
return false
}
}

Page.ss -
...
<% if colNumber %>
<!-- If colNumber returns true place markup for 2 column layout. -->
<% else %>
<!-- colNumber returned false so place markup for 3 column layout -->
<% end_if %>
...

Depending on how your page types and site tree are structured as well as what criteria you are using to determine what layout should be used you might be better off using separate template files like Mo described.

Avatar
honeybunny

Community Member, 79 Posts

31 October 2009 at 11:45am

I admit that I am still not full swing with the whole cms thing, but you've lost me. Instead of creating two different pages, one for 2-column and another for 3-column, dela11 wants to automate one page so that the user can choose from the cms whether it is a 2-column or 3-column layout. (Did I get this part right?) I'm sure that I'm not seeing the big picture, but is this a better choice than just creating two separate pages and choosing which page layout you want from the add page drop-down, or just another option? Or did I totally misunderstand the original post? (If it's door #3 then I will smile sheepishly and hope nobody noticed my ignorance.)

Thanks!

Avatar
JoshuaLewis

Community Member, 76 Posts

31 October 2009 at 12:31pm

@honeybunny - I didn't pick up on anything in the OP about the layout needing to be explicitly set in the CMS admin, although the template code dela11 posted does choose which layout to use based on page url and the site tree structure.

I think that the question of what is the best solution to providing the two different layouts is dependent on details of the problem that only dela11 can share with us.