tags:

views:

39

answers:

2

Im looking to sort a list of product names being output by Smarty. Here is the current code:

{foreach from=$products key=i item=product}
  <li>
      <a href="discuss.php?product={$product.uri}
      {if $filter_style}&amp;style={$filter_style}{/if}">{$product.name|capitalize}
      </a>
  </li>
{/foreach}

The HTML output:

<li>zzzzz</li>
<li>qqqqq</li>
<li>ccccc</li>
<li>aaaaa</li>  

How can I sort A-Z?

+1  A: 

If you want to do this inside the template rather than in the PHP which assigns the array, you can write a custom modifier for the array which sorts it in the foreach loop. See this blog post for an example

Paul Dixon
I've tried that it doesnt work.
danit
+1  A: 

Well, it is possible but this is not the perfect solution:

{php}
   sort($this->_tpl_vars['your_smarty_variable_name']);
{/php}
{foreach...

If you want to do it anyway in smarty it would be much easier (and elegant) to write a smarty plugin.

btw. the {php} {/php}tags in smarty 3 are deprecated

Zsolti