LemonStand Documentation

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.

Add your comment

Loading form...