Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » $FirstLast Problem when inside if statement [solved]

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

Page: 1
Go to End
Author Topic: 336 Views
  • Phils
    Avatar
    Community Member
    10 Posts

    $FirstLast Problem when inside if statement [solved] Link to this post

    Hi everybody,

    I have a Problem with $FirstLast inside a self made if statement.
    To assign a Page to a certain Menu on my Webpage I created some SettingsFields like:
    CheckboxField("ShowInFooterMenu", "Footer")

    Everything works fine except that when I create my Footer Menu like:

    <% loop $Menu(1) %>
    <% if ShowInMenus %>
    <% if ShowInFooterMenu %>
    <li class="{$FirstLast}">
       <a href="$Link" title="$Title.XML">$MenuTitle.XML</a>
    </li>
    <% end_if %>
    <% end_if %>
    <% end_loop %>

    the FirstLast is not working correctly.

    In Silverstripe I got several Pages after my last Footer Page which are not shown in my Footer because of the unchecked ShowInFooterMenu. But FirstLast seems to count them anyway. Am I doing something wrong?

    I'm using silverstripe 3.1.2.

    Thanks in advance for any help,
    Phil

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: $FirstLast Problem when inside if statement [solved] Link to this post

    <% loop $Menu(1) %> will be returning all the pages to the view, you might need to filter the results in the page controller before they make it to the view, otherwise the number of returned pages to the view will be the number the $FirstLast takes as the truth.

    Using the ORM filter might do the trick here along side a template method in Page_Controller. Have a go and post back if you get stuck again.

  • Phils
    Avatar
    Community Member
    10 Posts

    Re: $FirstLast Problem when inside if statement [solved] Link to this post

    Hi Camfindlay,

    thanks very much. I solved it the way you suggested. Here is my Page_Controller Code:

    public function getFooterMenu(){
       $footerMenuPages = DataObject::get('Page', "ShowInFooterMenu = true");
       return $footerMenuPages ;
    }

    And that's what's in my template:

    <% loop FooterMenu %>
    <% if ShowInMenus %>
    <li class="{$FirstLast}">
    <a href="$Link" title="$Title.XML">$MenuTitle.XML</a>
    </li>
    <% end_if %>
    <% end_loop %>

    That works.

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: $FirstLast Problem when inside if statement [solved] Link to this post

    Great that you solved it, can you add a [solved] to your original thread title please.

    Fyi, you should also be using the SilverStripe 3.0/3.1 ORM (assuming you are using that version.

    Example:

    $footerMenuPages = DataObject::get('Page', "ShowInFooterMenu = true");

    //SHOULD BE...

    $footerMenuPages = Page::get()->filter('ShowInFooterMenu',1);

    Be sure to read over the ORM/datamodel documentation: http://doc.silverstripe.org/framework/en/topics/datamodel

    I think there is also a way to get Pages that are actually published too so you don't leak any unpublished pages (start a new forum question for that if need be or search to see if others have already solved it).

  • Phils
    Avatar
    Community Member
    10 Posts

    Re: $FirstLast Problem when inside if statement [solved] Link to this post

    Thank you for your Example. You're right, that would be the better way in SS 3.1 which I'm using.

    It seems that SS 3 only gets the published Pages by default when using

    Page::get()->filter($filter);

    See also:

    [url=http://www.silverstripe.org/data-model-questions/show/21170]http://www.silverstripe.org/data-model-questions/show/21170

    I tested it, and it worked.

    At the end, to get the <% if ShowInMenus %> out of my template I used an Array to filter like this:

    public function getFooterMenu(){
    $footerMenuPages = Page::get()->filter(array(
       'ShowInFooterMenu' => 1,
       'ShowInMenus' => 1
    ));
    return $footerMenuPages;
    }

    Tanks again for your help and I'm gonna add a solved.

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