Ordering $file by name - Smart templates
I wonder if anyone can help me. I'm using PodHawk - a basic podcast cms, I would like the admin area file select to show my files in order - by name.
The select/option dropdown uses this code, is there a straightforward way to get the dropdown to display in order by name,. I've searched but cant find anything in the Smarty documentation, but I'm probably using the wrong terminology!
{foreach from=$upload item=file}
<option value="{$file|escape:'url'}">{$file}</option>
{/foreach}
many thanks rob
Solved with many thanks to poster below -
{$upload|@sort:$sma开发者_运维技巧rty.const.SORT_NUMERIC}
{foreach from=$upload item=file}
<option value="{$file|escape:'url'}">{$file}</option>
{/foreach}
cgwyllie neglected that asort() returns a boolean, not the sorted array. So his approach wouldn't work. As the index is not used, a(ssociative)sort is not required.
{$_foo = $upload|sort:$smarty.const.SORT_LOCALE_STRING}
{foreach $upload as $file}
<option value="{$file|escape:'url'}">{$file|escape:"html"}</option>
{/foreach}
should do the trick. Make sure you really need that $file urlencoded, otherwise change escape:"url" to escape:"html".
(the above is Smarty3 syntax)
If the variable $upload
contains an array of file names, then it should be possible to apply the PHP asort
function (http://php.net/asort) to the array as a smarty modifier.
{foreach from=$upload|@asort item=file}
<option value="{$file|escape:'url'}">{$file}</option>
{/foreach}
The @ symbol is needed to apply the modifier to the array as a whole, and not to each individual element. (See http://www.smarty.net/docsv2/en/language.modifiers.tpl)
If the array is of more complex data structures than just strings, the following discussion may be of use to you: http://www.smarty.net/forums/viewtopic.php?t=1079&postdays=0&postorder=asc&start=0
Edit
As mentioned by rodneyrehm, this solution is not quite correct although the poster found a satisfactory solution at: http://www.smarty.net/forums/viewtopic.php?t=1079&postdays=0&postorder=asc&start=0
精彩评论