Creating site maps, dynamic menus and breadcrumbs
LemonStand includes features for generating site maps and dynamic menus. Using these features you can display a whole site map or only subpages of a specific page. Displaying site maps and menus involves partials. It gives you unlimited power in customizing the markup and content of your website navigation.
We will demonstrate the navigation building features with simple tutorials.
Managing the page hierarchy and page navigation visibility
Pages in LemonStand can be grouped, forming a page tree. The page hierarchy is taken into account only during the site maps or dynamic menu generation. The page hierarchy does not affect page URLs.

You can manage page navigation behavior on the Navigation tab of the Create/Edit Page page. On this page you can select a page parent, specify whether the page should be visible in automatically generated menus and specify the page menu label. The page list on the CMS/Pages page display pages as a tree, allowing you to expand and collapse nodes.

By default pages in the site map and other menus are sorted in the order creation time. You can manage page order manually. Click the Manage page order button on the CMS/Pages page to open the order management tool. On this page you can drag pages up and down using the page icons as handles.

Displaying a full site map
Let's create a simple list-based website map. First, create a new page and assign it some title and URL, for example /site_map. This page will contain the website map. As the site map will be based on lists, we will need a UL element. Also, we will need a partial, which will display elements of the website map. Insert the following code to the Content field of the site map page:
<ul>
<? $this->render_partial('sitemap_pages', array('pages'=>$this->page->navigation_root_pages())) ?>
</ul>This code triggers the sitemap_pages partial rendering and passes a list of the root website pages to it. The list of root pages is obtained using the navigation_root_pages() method of the Cms_Page object, which is always accessible through the $page field of the Cms_Controller class (the $this variable).
Now we need to create the sitemap_pages partial. Create a partial with this name and paste the following code to the HTML Code field:
<? foreach ($pages as $page): ?> <li> <a href="<?= $page->url ?>"><?= h($page->navigation_label()) ?></a> </li> <? endforeach ?>
The code iterates through the $pages array which we passed from the site map page. For each page the code outputs a link and a title. The page title is obtained using the navigation_label() method. This method returns a Menu Label field of a page object. In case if the Menu Label field is empty, the method returns the page title.
Save the partial and navigate to the site map page. You will see a list of the website root pages.

Displaying nested pages
You can notice that the site map does not display nested pages. In order to display nested pages we need to extend the partial, making it render itself or each page, in case the page has subpages. This method is called recursion in programming.
<? foreach ($pages as $page): ?>
<li>
<a href="<?= $page->url ?>"><?= h($page->navigation_label()) ?></a>
<?
$subpages = $page->navigation_subpages();
if ($subpages):
?>
<ul>
<?= $this->render_partial('sitemap_pages', array('pages'=>$subpages)) ?>
</ul>
<? endif ?>
</li>
<? endforeach ?>The code loads a list of subpages of each page:
$subpages = $page->navigation_subpages();
Then checks whether subpages exist, and if so, outputs the UL element and then renders the partial again, passing the list of subpages to it. If you refresh the site map page, you will see that now it contains subpages.

Displaying subpages of a specific page
Similarly to the method described above, you can output subpages of a specific page, instead of displaying a full website map. Let's suppose you have the About page with the '/about' URL, and this page has subpages. We want to output the subpages of the About page.
<?
$about_page = Cms_Page::create()->find_by_url('/about');
$this->render_partial('sitemap_pages', array('pages'=>$about_page->navigation_subpages()))
?>You can use this code in a page, where you want to output a list of the About page subpages. The code loads the About page. Then it renders the partial which we created in the site map tutorial. But instead of passing a list of root pages to the partial, we now passing the subpages of the About page.

Generating breadcrumbs
The page hierarchy information can be used for generating dynamic breadcrumbs. You can use the following code for displaying the UL element with links to the parent pages of a current page.
<? $parents = $this->page->navigation_parents(); foreach ($parents as $parent): ?> <li><a href="<?= $parent->url ?>"><?= $parent->navigation_label() ?></a></li> <? endforeach ?>
The code calls the navigation_parents() method of the current page. This method returns a list of the page parents in reversed order. Then the code iterates through the list of the page parents and outputs a link and a page label for each parent. You can place this code into a template in order to have automatically generated breadcrumbs on each page of your website.

Previous: Page Not Found (404) page
Return to Creating pages


Comments
Theron Luhn
Wednesday, July 28, 2010$page->url doesn't output a correct URL if your lemonstand install isn't located in the root directory of your site. Use root_url($page->url) instead.
Gabe Couch
Wednesday, September 01, 2010Is there a way to show categories and sub categories of a product under the site map ? Also was wanting to display the sub categories of category. ( When you are viewing the category Accessories, displaying the sub categories of Accessories in a list. )
Aleksey Bobkov
Wednesday, September 01, 2010@Gabe Couch - this approach works only with CMS pages. It cannot display Product categories, product custom groups, manufacturers, blog categories, etc. But if you are a developer, you can implement some custom of generating the site map.
Regarding the subcategories - please read this forum thread: http://forum.lemonstandapp.com/topic/550/child-categories/
anhtu
Tuesday, May 31, 2011Hi, I created a subpage and wanted to know if there is a system wide menu system (ie. top menu) that show the subpages?
Aleksey Bobkov
Tuesday, May 31, 2011@anhtu - you should implement the front-end menu with the methods, described in this article. Basically you can fetch the top-level pages with the $this->page->navigation_root_pages() call and then fetch sub-pages for each page with $page->navigation_subpages() call.
Andrew
Thursday, July 14, 2011any way to show subcategories/pages only if the parent has been clicked?
Aleksey Bobkov
Thursday, July 14, 2011@andrew - Hi Andrew!
This topic has been discussed several times on the forum. For example: http://forum.lemonstandapp.com/topic/1411-hide-sub-categories-until-parent-category-clicked/
Basically you should use the is_current() method of the Shop_Category class.
Brett
Wednesday, August 03, 2011Hmmmm...... tried the code above but a var_dump() reveals that the result of "$this->page->navigation_parents()" is an empty array. I am on a product page.
Aleksey Bobkov
Wednesday, August 03, 2011@Brett Please make sure that the page has a parent selected on the Navigation tab. Also note that this menu building approach does not take into account any Shop module objects (product categories, etc). Please see my first response to @Gabe Couch.
gav
Friday, January 13, 2012i would suggest that you pass $page->url to the root_url function. This will make it work is the site is in a subfolder.
<a href="<?= root_url($page->url) ?>"><?= h($page->navigation_label()) ?></a>
Add your comment
Loading form...