Jump to:

23545 Posts in 19310 Topics by 2888 members

General Questions

SilverStripe Forums » General Questions » How do I code OR statement for Template use?

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

Page: 1
Go to End
Author Topic: 1427 Views
  • dela11
    Avatar
    Community Member
    1 Post

    How do I code OR statement for Template use? Link to this post

    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.

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: How do I code OR statement for Template use? Link to this post

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

  • JoshuaLewis
    Avatar
    Community Member
    76 Posts

    Re: How do I code OR statement for Template use? Link to this post

    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.

  • honeybunny
    Avatar
    Community Member
    79 Posts

    Re: How do I code OR statement for Template use? Link to this post

    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!

  • JoshuaLewis
    Avatar
    Community Member
    76 Posts

    Re: How do I code OR statement for Template use? Link to this post

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

    1427 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.