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.

Template Questions /

$FirstLast Problem when inside if statement [solved]


Reply


5 Posts   418 Views

Avatar
Phils

Community Member, 10 Posts

24 April 2014 at 2:39am

Edited: 24/04/2014 2:41am

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

Avatar
camfindlay

Forum Moderator, 181 Posts

24 April 2014 at 2:58am

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

Avatar
Phils

Community Member, 10 Posts

24 April 2014 at 7:44pm

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.

Avatar
camfindlay

Forum Moderator, 181 Posts

24 April 2014 at 9:12pm

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

Avatar
Phils

Community Member, 10 Posts

25 April 2014 at 7:59pm

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
[/url]

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.