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>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 ?>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') ?> 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')) ?>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 ?>See also:
Next: Category Page
Previous: Building your online store
Return to Building your online store


Comments
Neil Mills
Monday, September 05, 2011Is it possible to paginate a category child list?
Aleksey Bobkov
Monday, September 05, 2011@Neil Mills - standard tree-manipilation methods (list_children(), list_root_children()) do not support pagination. However it is still possible to paginate any ActiveRecord based objects if you use ActiceRecord methods. For example, you can load a list of the category children as follows: $categories = Shop_Category::create()->where('category_id is not null and category_id=?', $parent_category->id); After that you can paginate the $categories object and then call its find_all() method.
Add your comment
Loading form...