Jump to content

MarkupSimpleNavigation


Soma

Recommended Posts

When rendering two navs on the same page it seems that the first interferes with the second.

If my first nav is...

$menu = $modules->get("MarkupSimpleNavigation");
$items = $pages->find("parent=news");
echo $menu->render(null, null, $items);

...and then my second nav is...

echo $menu->render();

...then that second nav only shows children of news even though no PageArray is passed in to the render method.

Even if my second nav is...

$second_menu = $modules->get("MarkupSimpleNavigation");
echo $second_menu->render();

...it still seems to be limited by the PageArray of the first nav.

Can someone explain where I'm going wrong here? Thanks.

Link to comment
Share on other sites

I think I see what is causing this but don't fully understand yet as with a new instance it shouldn't be limited by the first. A second call with no arguments it should default to "null" but looks like it's somehow saved but don't get how this happens.

In my quick test It's only if you use a PageArray as root page. If using only 1 page as the root it doesn't happen. 

So for a quick solution you just have to explicit set the root page or items.

Link to comment
Share on other sites

Hello,

I've been testing MarkupSimpleNavigation for the first time yesterday.

I had been testing some pieces of code found in the ProcessWire Forums and in some profiles just before deciding to finally try it.

But for the moment, I'm stuck with the same obstacles.

I have a first horizontal menu with only the homepage and its children(1) (I will see if I remove the homepage later).

Just below I would like a second horizontal menu with the children(2) of a child(1) appearing when their parent is active.

But, I would need the children(2) not to disappear when clicking on one of them.

Is it possible, and without having to create a different template?

Thanks in advance for some help.

Here is the current second menu (but I've tried several variations with the first and second menus):

<?php
        if($page->template != "home") {
            $subMenu = $modules->get("MarkupSimpleNavigation"); // load the module
            $rootPage = $page;
            echo $subMenu->render(null, null, $rootPage); // render default submenu           
        }
    ?>
Link to comment
Share on other sites

you have no issue here - you've just say the module what pages to show....so if you have a spitted menu with

topmenu 

_______ |

content   | sidebarmenu

               |

you could switch to show childpages to siblings so the submenu is there if you select a subpage of a mainmenu item.

//sideBarMenuNav change items to view 
if($page->hasChildren) {  //we are on a first level parent page so show sencond level
$entries = $page->children;
} else {   //we are on second level so show siblings to actual page
$entries = $page->siblings;
};

using the menu i always show all options to easy change settings and/or see at first glance what is set...

//just a example from one of my websites...see max_levels, show_root and other options here
//markup is fitting for a bootstrap pill subnavigation...

$sideBarMenu = $modules->get("MarkupSimpleNavigation"); // load the module
	$options = array(
		'parent_class' => 'parent',		// overwrite class name for current parent levels
		'current_class' => 'active',	// overwrite current class
		'has_children_class' => 'has_children',    // overwrite class name for entries with children
		'levels' => true,    // wether to output "level-1, level-2, ..." as css class in links
		'levels_prefix' => 'level-',    // prefix string that will be used for level class
		'max_levels' => 2,    // set the max level rendered
		'firstlast' => false,    // puts last,first class to link items
		'collapsed' => false,    // if you want to auto-collapse the tree you set this to true
		'show_root' => false,    // set this to true if you want to rootPage to get prepended to the menu
                'selector' => 'template!=settings',    // define custom PW selector, you may sanitize values from user input
		'selector_field' => 'nav_selector',    // string (default 'nav_selector') define custom PW selector by using a property or field on a page. Use this setting if you want to overwrite the default nav_selector
		'outer_tpl' => '<ul class="nav nav-pills nav-stacked">||</ul>',    // template string for the outer most wrapper. || will contain entries
		'inner_tpl' => '<ul>||</ul>',    // template string for inner wrappers. || will contain entries
		//'list_tpl' => '',    // template string for the items. || will contain entries, %s will replaced with class="..." string
		'list_field_class' => '',  // string (default '') add custom classes to each list_tpl using tags like {field} i.e. {template} p_{id}
		'item_tpl' => '<a href="{url}#start">{title}</a>',    // template string for the inner items. Use {anyfield} and {url}, i.e. {headline|title}, if field is of type image it will return url to image (first image if multiple)
		'item_current_tpl' => '<a href="{url}#start">{title}</a>',    // template string for the active inner items.
		'xtemplates' => '',    // specify one or more templates separated with a pipe | to use the xitem_tpl and xitem_current_tpl markup
		'xitem_tpl' => '',    // same as 'item_tpl' but for xtemplates pages, can be used to define placholders
		'xitem_current_tpl' => '',    // same as 'item_current_tpl' but for xtemplates pages
		'date_format' => 'Y/m/d',    // default date formatting for Datetime fields and native created/modified
		'code_formatting' => true,    // enable or disable code indentations and newslines in markup output
		'debug' => false,    // show some inline information about rendertime and selectors used as html comments
	);
$sideBarNav = $sideBarMenu->render($options, null, $entries);

regards mr-fan

study all the options kindly (RTFM :rolleyes: ) before getting into an issue - this module with all the options is very very powerfull!

  • Like 1
Link to comment
Share on other sites

Hello Soma,

Where can version 1.3.4 be found?

Perhaps it can help solve my issue.

Have a nice day!

Ah sorry, it seems I haven't commited it yet or forgot it... It's now updated.

  • Like 1
Link to comment
Share on other sites

@mr-fan: I don't know why I didn't try with an if...else statement. Perhaps I had been too long on the computer...

I also have a commented version for the first menu.

@Cerulean: this time, just using $rootPage = $page->rootParent; is enough.

It's possible I used $rootParent here or somewhere else once without really knowing what I was doing, but then changed it as I was testing things in multiple tabs, and couldn't find it back later.

And perhaps I also did like just now: I forgot to upload the file again in FileZilla.

Thank you both!

I will also later look (if and) how I can use Drop Jumbo [ http://cferdinandi.github.io/drop/ ] with MarkupSimpleNavigation.

(The Drop Basic demo is going to be fixed. It seems that, since the new version and website were released a few days ago, it doesn't work anymore.)

Link to comment
Share on other sites

@soma

I was trying to add an image to the left of each navigation link.

As your docs mention, the following will grab the first image in a field I specify

'item_current_tpl' => '<div class="nav-icon"><img src="{images}"></div><a href="{url}">{title}</a>',

I had a few challenges here and wondered about best approach

Image Tag

My images field may contain several images. I could create a separate navigation_images field but would love to call an image by tag

Image size

I can accomplish by CSS but wondered if I could apply the API width() 

Ultimately, my markup would resemble something like this. It's the getTag and width(300) which break the call

'item_current_tpl' => '<div class="nav-icon"><img src="{images->getTag('icon')->width(300)->url}"></div><a href="{url}">{title}</a>',
Link to comment
Share on other sites

Wondered if I was along the right track with following.

I associate a variable with my image tag including width:

$myimage = $page->images->getTag('icon')->width(300)->url;

and then in the MarkupSimpleNavigation refer to $myimage in the item_crrent_tpl line

'item_current_tpl' => '
<div class="nav-icon"><img src="{myimage}"></div><a href="{url}">{title}</a>
',
Link to comment
Share on other sites

Eventually solved this with the help of a mate who is an experienced PHP developer.

<?php $treeMenu = $modules->get("MarkupSimpleNavigation");
		  
$myimage = $page->images->getTag('icon')->width(50)->url;	
		
$options = array(
'item_current_tpl' => '<div class="nav-icon"><img src="'.$myimage.'"></div><a href="{url}">{title}</a>',
; 
?>	

We both acknowledge that a better solution is most likely to use the Hook method outlined in the Module readme but I needed something quick (approaching deadline) that I could relate to and maintain myself.

Given time I may look more closely at the Hook method and try understand that a bit more.

Link to comment
Share on other sites

@Peter Knight - won't $myimage always be the icon for the current page?

$myimage = $page->images->getTag('icon')->width(50)->url;    

or does the $page context change inside the MSN loop? ... curious about your solution

Link to comment
Share on other sites

right, that's good to know (nevermind my dumb question)... i guess MSN changes the $page context, even though you set the variable outside of the options...in other words $page when in options means the menu item page, not the page being viewed;

Link to comment
Share on other sites

Eventually solved this with the help of a mate who is an experienced PHP developer.

<?php $treeMenu = $modules->get("MarkupSimpleNavigation");
		  
$myimage = $page->images->getTag('icon')->width(50)->url;	
		
$options = array(
'item_current_tpl' => '<div class="nav-icon"><img src="'.$myimage.'"></div><a href="{url}">{title}</a>',
; 
?>	

We both acknowledge that a better solution is most likely to use the Hook method outlined in the Module readme but I needed something quick (approaching deadline) that I could relate to and maintain myself.

Given time I may look more closely at the Hook method and try understand that a bit more.

That's seems like a viable solution, but only for the active current page. You also just set the "item_current_tpl" and that's only for the current active page. If that's the idea that's ok. 

MarkupSimpleNavigation doesn't change any page context at all. So the $page you have there's is only the current page viewing.

When working with a hook, you would have to check for current page with

if($child === wire("page")) {
    // do your markup logic
}

Taking the example from the readme I linked earlier it would be like:

$nav = $modules->get("MarkupSimpleNavigation");

function myItemString(HookEvent $event){
    // the current rendered child page
    $child = $event->arguments('page'); 
    
    // if current child you're viewing
    if($child === wire("page")) { 
        $myimage = $child->images->getTag('icon')->width(50);
        $itemMarkup = "<div class='nav-icon'><img src='{$myimage->url}'></div><a href='{$child->url}'>{$child->title}</a>";
        $event->return .= $itemMarkup; // send back the markup that will present a item
    }
}

// setup the hook
$nav->addHookAfter('getItemString', null, 'myItemString');

// a render will then also trigger the hook above on each item
echo $nav->render(); 

This would allow to have all different logic in your output depending on custom criterias

  • Like 2
Link to comment
Share on other sites

Hi - Can someone tell me what options I would need to modify to get MSN to output below menu?

  <select id="mobile-nav">
                  <option>Select page:</option>

                  <option value="index.html">Home</option>
                    <option value="index.html">  Default Layout</option>
                  <option value="about.html">About</option>
                  <option value="blog_right.html">Blog</option>
                    <option value="blog_right.html">  Right sidebar - 1</option>
                    <option value="blog_right2.html">  Right sidebar - 2</option>
                    <option value="blog_post.html">  Blog Post</option>
        <option value="simplecodes_columns.html">Simple Codes</option>
          <option value="simplecodes_columns.html">  Columns</option>
        <option value="portfolio_three1.html">Portfolio</option>
          <option value="portfolio_one1.html">  One Column 1</option>
          <option value="portfolio_three1.html">  Three Columns 1</option>
        <option value="contact.html">Contact</option>

                </select>
Link to comment
Share on other sites

Seems like a weird menu or I don't get it. If it's for a menu, why it needs a "Select page"?

I'm not sure MSN is made for that task if it's a nested menu structure cause it used UL//OL style nesting that isn't the same as a Select menu would require. I haven't tried but maybe something like this works for a single level menu.

$nav = $modules->MarkupSimpleNavigation;

$options = array(
    "outer_tpl" => "||",
    "list_tpl" => "||",
    "item_tpl" => "<option value='{url}'>{title}</option>",

);

$content .= "<select id='mobile-nav'>";
$content .= "<option>Select</option>";
$content .= $nav->render($options);
$content .= "<select id=''>";

There's JS plugins that can make a Select menu out of a UL list menu.

Link to comment
Share on other sites

That's seems like a viable solution, but only for the active current page. You also just set the "item_current_tpl" and that's only for the current active page. If that's the idea that's ok. 

Macrura and Soma - you're both correct. My code only works for the current page. I hadn't realised earlier as I was testing and viewing a single page which just happened to be my current page :-/

Moving on, I've worked Somas example into my template, updated the field names etc to produce the following.

As Soma mentioned, this displays an icon only for the current page.

<?php $nav = $modules->get("MarkupSimpleNavigation");

function myItemString(HookEvent $event){
    // the current rendered child page
    $child = $event->arguments('page'); 
    
    // if current child you're viewing
    if($child === wire("page")) { 
        $myimage = $child->nav_icon;
        $itemMarkup = "<div class='nav-icon'><img src='{$myimage->url}'></div><a href='{$child->url}'>{$child->title}</a>";
        $event->return .= $itemMarkup; // send back the markup that will present a item
    }
}

// setup the hook
$nav->addHookAfter('getItemString', null, 'myItemString');

// the navigation options
$options = array(

  'parent_class' => 'parents',
  'current_class' => 'current',
  'has_children_class' => 'has_children',
  'levels' => true,
  'levels_prefix' => 'level-',
  'max_levels' => 1,
  'firstlast' => true,
  'collapsed' => false,
  'show_root' => true,
  'selector' => '',
  'selector_field' => 'nav_selector',
  'outer_tpl' => '<ul class="sidelist">||</ul>',
  'inner_tpl' => '<ul>||</ul>',
  'list_tpl' => '<li%s >||</li>',
  'list_field_class' => '',
  'item_tpl' => '<a href="{url}">{title}</a>',
  'item_current_tpl' => '<a href="{url}">{title}</a>',
  'xtemplates' => '',
  'xitem_tpl' => '<a href="{url}">{title}</a>',
  'xitem_current_tpl' => '<span>{title}</span>',
  'date_format' => 'Y/m/d',
  'code_formatting' => false,
  'debug' => false
);

$root = $page->rootParent; // Start from the top root parent of current page
// a render will then also trigger the hook above on each item
echo $nav->render($options, null, $root); 
?>	

The key to getting my menu to produce a unique icon per page seemed to be removing the if($child === wire("page"))

function myItemString(HookEvent $event){
    // the current rendered child page
    $child = $event->arguments('page'); 
    
    
        $myimage = $child->nav_icon;
        $itemMarkup = "<div class='nav-icon'><img src='{$myimage->url}'></div><a href='{$child->url}'>{$child->title}</a>";
        $event->return .= $itemMarkup; // send back the markup that will present a item
   
}

My only problem at the moment is that each navigation link is being output twice.

post-1166-0-18507800-1441472175_thumb.pn

Link to comment
Share on other sites

Ok I got it to work.

I realised I had to zap the contents of both 'item_tpl' and item_current_tpl' 

My final working code is as follows.

Apologies if I'm filling up this thread with my own hacked code and examples. Just hoping it's useful to someone in the future.

<?php 

$nav = $modules->get("MarkupSimpleNavigation");

function myItemString(HookEvent $event){
    $child = $event->arguments('page'); // current rendered child page
    // any logic with $child possible here
    	
		$myimage = $child->nav_icon;
        // set the return value of this hook to a custom string
        $event->return .= "<div class='nav-icon'><img src='{$myimage->url}'></div><a href='$child->url'>$child->title</a>";
   
}


$options = array(
				'parent_class' => 'parents',
				'current_class' => 'current',
				'has_children_class' => 'has_children',
				'levels' => true,
				'levels_prefix' => 'level-',
				'max_levels' => 1,
				'firstlast' => true,
				'collapsed' => false,
				'show_root' => true,
				'selector' => '',
				'selector_field' => 'nav_selector',
				'outer_tpl' => '<ul class="sidelist">||</ul>',
				'inner_tpl' => '<ul>||</ul>',
				'list_tpl' => '<li%s >||</li>',
				'list_field_class' => '',
				'item_tpl' => '',
				'item_current_tpl' => '',
				'xtemplates' => '',
				'xitem_tpl' => '<a href="{url}">{title}</a>',
				'xitem_current_tpl' => '<span>{title}</span>',
				'date_format' => 'Y/m/d',
				'code_formatting' => false,
				'debug' => false
			);
$root = $page->rootParent; // Start from the top root parent of current page
// setup the hook after on ___getItemString($class, $page) method
$nav->addHookAfter('getItemString', null, 'myItemString');
echo $nav->render($options, null, $root);

?>

Link to comment
Share on other sites

Sorry little late at the party but like i wrote a little how to use MSN for a megamenu or add any other items between items...

this should be the result - some li item with extra div content with latest/top article and some other items in this example from a pagefield/categories....

post-2327-0-50642100-1441473527_thumb.pn

Code ist hopefully well commented...

//topMenuNav
$rootPage = $page->get('/'); //shows current tree
$topMenu = $modules->get("MarkupSimpleNavigation"); // load the module

// hook to have custom items markup
$topMenu->addHookAfter('getTagsString', null, 'customNavItems');

function customNavItems(HookEvent $event){
    $item = $event->arguments('page');
    // get the article mainnav item and add some megamenu items...
    if($item->id == 1068){

        //build the normal li item output
        $out    = '<a href="'.$item->get("url").'">'.$item->get("title|name").'</a>';

        //get the last article
        $top_article = wire('pages')->find("template=artikel,top_thema=1,sort=-publish_from")->first();
        //get the last article
        $latest_article = wire('pages')->find("template=artikel,sort=-publish_from")->first();

        //get image thumb from $top_article
        //get the original image
        $imagetop = $top_article->get("artikel_bild");
        // get the image instance of the cropped version of "artikel" 750px wide
        $thumbtop = $top_article->artikel_bild->getCrop('Artikel');

        //get image thumb from $latest_article
        //get the original image
        $imagelatest = $latest_article->get("artikel_bild");
        // get the image instance of the cropped version of "artikel" 750px wide
        $thumblatest = $latest_article->artikel_bild->getCrop('Artikel');

        //get the items for the tagmenu
        $cat_items = wire('pages')->find("template=artikel_cat");

        //get the rootpage of the posts
        $artikel_root = wire('pages')->get(1068);
        //homepageurl
        $homepageurl = wire('pages')->get('/')->httpUrl;

        //start megamenu
        $out    .= '<ul class="mega">';
        //first column last "top" marked article
        $out    .= '<div class="one_third"><h4 class="subtitle">Top Aktuell</h4>';
        $out        .= '<a class="mega-link-img" href="'.$top_article->get("url").'"><img src="'.$thumbtop->url.'" alt="'.$thumbtop->description.'"><br></a>';
        $out        .= '<h6><strong>'.$top_article->get("headline").'</strong></h6>';
        $out        .= '<p>'.$top_article->get("shorttext").'</p>';
        $out    .= '</div>';
        //second column - latest article
        $out    .= '<div class="one_third"><h4 class="subtitle">Letzter Artikel</h4>';
        $out        .= '<a class="mega-link-img" href="'.$latest_article->get("url").'"><img src="'.$thumblatest->url.'" alt="'.$thumblatest->description.'"><br></a>';
        $out        .= '<h6><strong>'.$latest_article->get("headline").'</strong></h6>';
        $out        .= '<p>'.$latest_article->get("shorttext").'</p>';
        $out    .= '</div>';
        //third column - list of tags (links to URL segments on artikel_stamm template)
        $out    .= '<div class="one_third"><h4 class="subtitle">Themenbereiche</h4>';
            foreach ($cat_items as $cat) {
                $out .= '<li><a href="'.$homepageurl.$artikel_root->name.'/'.$cat->name.'/">'.$cat->title.'</a></li>';
            }
        $out    .= '</div>';
        //close megamenu
        $out    .= '</ul>';
        $event->return = $out;
    }
}

$options = array(
    'parent_class' => 'parent',        // overwrite class name for current parent levels
    'current_class' => 'current',    // overwrite current class
    'has_children_class' => 'has_children',    // overwrite class name for entries with children
    'levels' => true,    // wether to output "level-1, level-2, ..." as css class in links
    'levels_prefix' => '',    // prefix string that will be used for level class
    'max_levels' => 3,    // set the max level rendered
    'firstlast' => false,    // puts last,first class to link items
    'collapsed' => false,    // if you want to auto-collapse the tree you set this to true
    'show_root' => false,    // set this to true if you want to rootPage to get prepended to the menu
    'selector' => 'template!=artikel|event',    // define custom PW selector, you may sanitize values from user input
    'selector_field' => 'nav_selector',    // string (default 'nav_selector') define custom PW selector by using a property or field on a page. Use this setting if you want to overwrite the default nav_selector
    'outer_tpl' => '<ul id="nav" class="sixteen columns">||</ul>',    // template string for the outer most wrapper. || will contain entries
    'inner_tpl' => '<ul>||</ul>',    // template string for inner wrappers. || will contain entries
    //'list_tpl' => '',    // template string for the items. || will contain entries, %s will replaced with class="..." string
    'list_field_class' => '',  // string (default '') add custom classes to each list_tpl using tags like {field} i.e. {template} p_{id}
    'item_tpl' => '<a href="{url}">{title}</a>',    // template string for the inner items. Use {anyfield} and {url}, i.e. {headline|title}, if field is of type image it will return url to image (first image if multiple)
    'item_current_tpl' => '<a href="{url}">{title}</a>',    // template string for the active inner items.
    'xtemplates' => '',    // specify one or more templates separated with a pipe | to use the xitem_tpl and xitem_current_tpl markup
    'xitem_tpl' => '',    // same as 'item_tpl' but for xtemplates pages, can be used to define placholders
    'xitem_current_tpl' => '',    // same as 'item_current_tpl' but for xtemplates pages
    'date_format' => 'Y/m/d',    // default date formatting for Datetime fields and native created/modified
    'code_formatting' => true,    // enable or disable code indentations and newslines in markup output
    'debug' => false,    // show some inline information about rendertime and selectors used as html comments
);

$topMenuMarkup = $topMenu->render($options, null, $rootPage);

It's long and detailed i always like to see how all options of the Menu are set....so options could be less.

Hope this works for others as example how to get there with MarkupSimpleNavigation...on such topics.

I like this module very much since in my former CMS there was a great function for generating menus, too. With this kind of generating markup for navigation it is not always easy but it is always very powerfull and if you've it running it a solid solution ;)

Best regards mr-fan

  • Like 2
Link to comment
Share on other sites

if you take my example from above you could strip it to the minimal setup like this:

//topMenuNav
$rootPage = $page->get('/'); //shows current tree
$topMenu = $modules->get("MarkupSimpleNavigation"); // load the module

// hook to have custom items markup
$topMenu->addHookAfter('getTagsString', null, 'customNavItems');

function customNavItems(HookEvent $event){
    $item = $event->arguments('page');

    //if the item id is 1068 do something

    if($item->id == 1068){

        //build the normal li item output or change the item with id 1068 to your needs
        //add new items that aren't in the normal pagetree or add megamenu items
        //....do what you want!
        
        $out    = '<a href="'.$item->get("url").'">'.$item->get("title|name").'</a>';

        $event->return = $out;
    }
}
//setup $options for the normal markup and render the nav...

so you could get every special item you need and change it.

best regards mr-fan

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...