Jump to content


Photo

Display Menu using Selectors and Checkboxes

checkbox selector menu visibility

  • Please log in to reply
9 replies to this topic

#1 ptjedi

ptjedi

    Full Member

  • Members
  • PipPipPip
  • 51 posts
  • 7

  • LocationPortugal

Posted 23 May 2012 - 01:04 PM

Greetings all,

I am trying to separate which pages display on the header menu and those which appear on footer menu. For that I created two checkbox fields, applied them to the templates, and now I am trying to filter them in the menu list.

Here's my code:

<?php
    [b]$homepage = $pages->get("/")->find("visibility_in_top_menu=1");[/b]
    $children = $homepage->children;
    // $children->prepend($homepage);
   
    $i = count($children)-1;
    foreach($children as $child) {
	  $class = $child === $page->rootParent ? " class='on'" : '';
	  if($i>0){echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";}
	  else {echo "<li><a$class href='{$child->url}' id='last'>{$child->title}</a></li>";}
	  $i-=1;
    }
   ?>

The thing is that I am probably not setting up the first line well.
Can anybody help me out?

Thanks!

#2 diogo

diogo

    Hero Member

  • Moderators
  • 1,998 posts
  • 1076

  • LocationPorto, Portugal

Posted 23 May 2012 - 01:24 PM

I think this is what you want

$homepage = $pages->get("/");
$children = $homepage->children->find("visibility_in_top_menu=1");


#3 ptjedi

ptjedi

    Full Member

  • Members
  • PipPipPip
  • 51 posts
  • 7

  • LocationPortugal

Posted 23 May 2012 - 02:54 PM

Perfect. Now I understand how it's done.
Many thanks!

#4 ptjedi

ptjedi

    Full Member

  • Members
  • PipPipPip
  • 51 posts
  • 7

  • LocationPortugal

Posted 23 May 2012 - 04:04 PM

One more thing:

I understand that the following line adds the "on" class to the menu item if the current page matches that item, however I don't understand how that works.

$class = $child === $page->rootParent ? " class='on'" : '';

Could you please explain it to me?

This is because I've changed pages level like this:

$homepage = $pages->get("/en/");

and now the on states aren't working...

Thanks

#5 ptjedi

ptjedi

    Full Member

  • Members
  • PipPipPip
  • 51 posts
  • 7

  • LocationPortugal

Posted 23 May 2012 - 04:09 PM

I think I got it even though I really don't understand why now this is needed.

Changed
$class = $child === $page->rootParent ? " class='on'" : '';
to
$class = $child === $page ? " class='on'" : '';


#6 diogo

diogo

    Hero Member

  • Moderators
  • 1,998 posts
  • 1076

  • LocationPorto, Portugal

Posted 23 May 2012 - 05:12 PM

$class = $child === $page->rootParent ? " class='on'" : '';

is a short way of writing

if($child === $page->rootParent) {
	$class = " class='on'";
} else {
	$class = '';
}


$page->rootParent gets the ancestor that is closer to the root (a direct children of the homepage). If you change the homepage variable to a something different from the root, they will never match. For this to work you have to replace $page->rootParent for this page's ancestor that is closer to /en/.
Put something like this after the first lines:

foreach($page->parents as $v) {
	if($children->has($v)) {
		$newRootParent = $v;
	}
}


Then replace $page->rootParent by $newRootParent.

I'm not on my computer, and I can't test this right now, so, no guarantees here :)

#7 ptjedi

ptjedi

    Full Member

  • Members
  • PipPipPip
  • 51 posts
  • 7

  • LocationPortugal

Posted 25 May 2012 - 11:04 AM

Many thanks for your support Diogo. It is working perfectly.

#8 Georgson

Georgson

    Distinguished Member

  • Members
  • PipPipPip
  • 53 posts
  • 31

  • LocationMunich

Posted 11 February 2013 - 04:01 PM

Thanks, diogo! Your post helped me to solve my (quite similar) problem. I'm still not used to the short version of this if-function - but the long version helped me understand it and to fill in the right pieces. 



#9 JeffS

JeffS

    Distinguished Member

  • Members
  • PipPipPip
  • 76 posts
  • 62

  • LocationMichigan - USA

Posted 13 February 2013 - 09:10 AM

@Georgson - That is called the ternary conditional operator. 

 

From the manual .. 

 

<?php
$first ? $second : $third
?>

 

 

If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.

 
For those interested read more at php.net ... 


#10 Soma

Soma

    Hero Member

  • Moderators
  • 3,192 posts
  • 1748

  • LocationSH, Switzerland

Posted 13 February 2013 - 10:27 AM

It can be better explained with this:

result = ifonething==something ? iftrue : iffalse;
 

@somartist | modules created | support me, flattr my work flattr.com






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users