Smarty Category Filtering

Until recently, I depended solely on the most excellent FilterCategories plugin to, well, filter my category list. For instance, where I might present a subset of my entire category list. The latest hurdle in a static-to-dynamic conversion is dealing with the problem that this plugin doesn’t work in a dynamic publishing environment.

To start my journey, I took an in-depth look at the MTCategories container within the PHP code. It turns out that this accepts a “label” parameter, designed to allow you to select one category of entries at a time. The most common use for this is probably when you submit the MTEntries container with a category attribute, but it would work for my needs.

Then I came to a problem. While the documentation for MTEntries appears to allow for multiple categories (ie, “Foo AND Bar”), that method doesn’t seem to work with dynamic publishing. So I had to figure out a way to feed it one category name at a time. Sounds like I need to loop over the categories to get what I need. Smarty to the rescue!

This led directly to my next problem – namely that Smarty doesn’t seem to like assigning arrays in the template. Not by default, anyway. Luckily, the Smarty engine is extendable, much like Movable Type, with plugins. That’s when I found the Advanced Assign Plugin. I promptly added this to my Movable Type installation and created a variable to hold my categories.

  {{assign_adv var="foo" value="array('bar','baz')"}}

Now that I have the list of categories, I just needed to loop over them to produce my page.

  {{foreach from="$foo" item="oof"}}
  <MTCategories label="`$oof`">

In the from attribute, I include the name of my array variable, created earlier ($foo in this case). Include the preceding dollar sign. In the item attribute, I provide the name of the variable to use in the loop (oof in this case). Do not include the dollar sign this time.

Once I have the foreach loop created, I simply use my normal MT tags – in this example, a basic MTCategories loop that does nothing at all. This is just to demonstrate how it would be used. In the label attribute of this container, you use the variable name provided by the item attribute earlier, but you use the preceding dollar sign, and you enclose the variable in backticks.

Using this method, you can loop over any arbitrary list of categories you like. Simply change the list of categories, and the output of the page will change along with it. If you need help setting up something like this on your site, we are available for Movable Type Consulting. Let us know how we can help!

2 Replies to “Smarty Category Filtering”

  1. What you seem to be missing is that there is no need to do any sort of selection direct from the DB whatsoever, which makes this available to those would like to do some extension of their templates, without requiring them to know SQL.

    Obviously Smarty knows arrays – the example is using one. I’m not exactly sure why making a more complex solution would be appropriate. But if I’m missing something that would make it more useful, by all means, please elaborate!

  2. Smarty actually LOVES arrays!

    In this situation what i would have done was :-
    – select DISTINCT category types from your DB
    – put them in an array and assign them to the template.
    – using the smarty {section} function, loop through them, displaying them, and linking to the diff categories.

    this will work for any number of categories, 100% dynamic

Comments are closed.