2011-03-29 62 views
0

我想在使用html_options生成html選項時使用string_format格式化輸出。在Smarty中輸出過濾器html_options

例如,我試圖讓選擇具有前導零:

<select> 
{$options=range(1,12)} 
{html_options values=$options output=$options|string_format:'%02d'} 
{/select} 

這給01如預期的第一選擇,但其餘的空白標籤。這應該基於this page(在該頁面上搜索'truncate')上的示例進行工作,所以我不確定我在做什麼錯誤。

回答

2

不知何故聰明的方法string_format將無法​​在陣列上工作。什麼可以做,雖然預先創建關聯數組:

{section name=foo start=1 loop=13} 
    {$options[$smarty.section.foo.index]=$smarty.section.foo.index|string_format:'%02d'} 
{/section} 

而且simly用它作爲options,而不是valuesoutput

<select> 
    {html_options options=$options} 
</select> 
相關問題