utilitees

- Developer Notes

Developer Notes


Text-indent nested category list

I once had the problem that I needed to create a SAEF for a weblog which has three category groups. When using the standard {category_menu}{/category_menu} tag-pair all groups are merged into one which was not appropriate because the category groups did not belong together thematically.

Hence I hardcoded the category menus to show up in three separate select-lists.

However one category group had several levels of subcategories and I wanted to show them properly nested like in the list below:

I ended up with a plugin that transforms the HTML-output of {exp:weblog:categories style=“nested”} into an indented text-only version.

To use the plugin just wrap its tags around a pair of {exp:weblog:categories}-tags.

The plugin will read its output into an array using xml_parse_into_struct(), remove the <ul></ul> and <li> </li> elements and prepend an indentation-string to the category_name according to the nesting-level.

Example:

<select name="category[]" size="8" multiple="multiple">
{exp:textnest fill="&amp;nbsp;&amp;nbsp;"}
{exp
:weblog:categories weblog="directory" category_group="2" style="nested"}
<option value="{category_id}">{exp:xml_encode}{category_name}{/exp:xml_encode}</option>
{/exp:weblog:categories}
{
/exp:textnest}
</select

Optional parameter:

fill="" 

Determines what string will be used to indent the subcategories. Defaults to &nbsp;.

P.S.: The output of {exp:weblog:categories} must be valid XML. If your categories may contain special characters, better enclose the {category_name} variable in {exp:xml_encode}{/exp:xml_encode}-tags.


You could also use this to indent the category selection drop-down menu described in the Knowledge Base.

<form name="catmenu" action="">
  <
select name="selcat"
onchange="location=document.catmenu.selcat.options[document.catmenu.selcat.selectedIndex].value;">
    <
option value="">--Select Category--</option>
{exp:textnest fill="&amp;nbsp;&amp;nbsp;"}
{exp
:weblog:categories weblog="yourweblog" style="nested" dynamic="off"}
    
<option value="{path=weblog}">{category_name}</option>
{/exp:weblog:categories}
{
/exp:textnest}
  
</select>
</
form

Download the plugin

Related entries:
MP3 Info + released (1 tags in common)
Splitter plugin updated (1 tags in common)
New plugin released (1 tags in common)
TruncHTML updated to v1.3 (1 tags in common)
TruncHTML (1 tags in common)

Leave a comment

Name:

Email:

URL:

Remember my personal information
Notify me of follow-up comments?