Smarty sort array
Im looking to sort a list of product names being output by Smarty. Here is the current code:
{f开发者_运维知识库oreach from=$products key=i item=product}
<li>
<a href="discuss.php?product={$product.uri}
{if $filter_style}&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?
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
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
精彩评论