CLiown
CLiown

Reputation: 13843

Smarty sort array

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?

Upvotes: 0

Views: 6142

Answers (2)

Zsolti
Zsolti

Reputation: 1607

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

Upvotes: 1

Paul Dixon
Paul Dixon

Reputation: 300845

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

Upvotes: 1

Related Questions