This is the documentation for LemonStand V1, which has been discontinued. You can learn more and upgrade your store here.

LemonStand Version 1 Has Been Discontinued

This documentation is for LemonStand Version 1. LemonStand is now offered as a cloud-based eCommerce platform.
You can try the new LemonStand and learn about upgrading here.

Displaying a List of Categories

You may need to display a list of all categories in your catalog, for example in a sidebar of your website.

Displaying a plain list of categories

You can use the following method if you are not going to use nested categories on your website. The code outputs only the root-level categories.

<? $categories = Shop_Category::create()->list_root_children(); ?>
<ul>
  <? foreach ($categories as $category):  ?>
    <li>
      <a href="<?= $category->page_url('/category') ?>"><?= $category->name ?></a>
    </li>
  <? endforeach; ?>
</ul>
{% set categories = method('Shop_Category', 'create').list_root_children() %}  
<ul>
  {% for category in categories  %}
    <li>
      <a href="{{ category.page_url('/category') }}">{{ category.name }}</a>
    </li>
  {% endfor %}
</ul>

Displaying a hierarchical list of categories

Categories in LemonStand are hierarchical. You can use nested lists to represent the hierarchy. To display the category hierarchy, you need to use a partial, instead of placing the code right into a page. That allows you to display subcategories using the same code as you use to display top-level categories. Also, using partials is the best practice from the code reusing point of view. Once you have developed a partial for displaying a list of categories, you can render the partial on any page.

To output a hierarchical list of categories, create a new partial and assign it some meaningful name, for example shop:categories. The following code snippet demonstrates the possible content of the partial.

<?   
  if (!isset($category_url_name))
    $category_url_name = null;

  $categories = isset($parent_category) ? $parent_category->list_children() : Shop_Category::create()->list_root_children();

  if ($categories->count): ?>
    <ul>
      <? foreach ($categories as $category):  ?>
        <li <? if ($category_url_name == $category->url_name): ?>class="current"<? endif ?>>
          <a href="<?= $category->page_url('/category') ?>"><?= $category->name ?></a>

          <? $this->render_partial('shop:categories', array('parent_category'=>$category)) ?>
        </li>
      <? endforeach; ?>
    </ul>
<? endif ?>
{% set categories = parent_category is defined ? 
     parent_category.list_children() : 
     method('Shop_Category', 'create').list_root_children() %}

{% if categories.count %}
  <ul>
    {% for category in categories  %}
    <li {% if category_url_name == category.url_name %}class="current"{% endif %}>
      <a href="{{ category.page_url('/category') }}">{{ category.name }}</a>
      {{ render_partial('shop:categories', {'parent_category': category}) }}
    </li>
    {% endfor %}
  </ul>
{% endif %}

Notice that this partial renders itself in line 12. This method is called recursion in programming. It allows you to use the same code for displaying the root-level and nested categories.

Now, having the partial developed, it is very easy to output a list of categories on any page:

<? $this->render_partial('shop:categories') ?> 
{{ render_partial('shop:categories') }}

The $catetory_url_name variable in the category list code is used for marking a current category. To mark a current category, pass category_url_name as a parameter in the render_partial method call.

<? $this->render_partial('shop:categories', array('category_url_name'=>'computers')) ?>
{{ render_partial('shop:categories', {'category_url_name': 'computers'}) }}

Needless to say, a value of category_url_name parameter could be dynamic. You can load the URL name of a current category on a category page, or from a product category on a product details page.

Managing the category order

LemonStand Administration Area allows you to manage the ordering of categories using a simple drag and drop technique. By default categories on the front-end website are ordered by name. If you want to output the categories accordingly the order you specified in the Administration Area, you should pass the 'front_end_sort_order' value to the category list_root_children and list_children methods. Below is an example code of the partial which we discussed above, which outputs categories in the order specified in the Administration Area. 

<?   
  if (!isset($category_url_name))
    $category_url_name = null;

  $categories = isset($parent_category) ? 
    $parent_category->list_children('front_end_sort_order') :
    Shop_Category::create()->list_root_children('front_end_sort_order');

  if ($categories->count): ?>
    <ul>
      <? foreach ($categories as $category):  ?>
        <li <? if ($category_url_name == $category->url_name): ?>class="current"<? endif ?>>
          <a href="<?= $category->page_url('/category') ?>"><?= $category->name ?></a>

          <? $this->render_partial('shop:categories', array('parent_category'=>$category)) ?>
        </li>
      <? endforeach; ?>
    </ul>
<? endif ?>
{% set categories = parent_category is defined ? 
     parent_category.list_children('front_end_sort_order') : 
     method('Shop_Category', 'create').list_root_children('front_end_sort_order') %}

{% if categories.count %}
  <ul>
    {% for category in categories  %}
    <li {% if category_url_name == category.url_name %}class="current"{% endif %}>
      <a href="{{ category.page_url('/category') }}">{{ category.name }}</a>
      {{ render_partial('shop:categories', {'parent_category': category}) }}
    </li>
    {% endfor %}
  </ul>
{% endif %}

See also:

Next: Creating a Category Page
Previous: Categories
Return to Categories