2009-11-07 50 views
2

是否可以在smarty中的配置文件中定義數組?例如我想要在配置文件(位於/ configs)中有小數據庫 - 很少(約20)產品說明:標題,價格,說明。之後,我想通過foreach或部分列出它。 如何在沒有MySql或其他數據庫引擎的Smarty中定義該數組。我可以這樣做嗎?通過配置文件在smarty中定義數組

回答

6

您可以在配置文件中定義數組,你需要設置$ config_overwrite = FALSE。 你可以看看這個頁面:config_overwrite

$smarty = new Smarty; 
$smarty->config_overwrite=false; 

的配置文件。

# row colors 
rowColors = #FF0000 
rowColors = #00FF00 
rowColors = #0000FF 

帶有{section}循環的模板。

<table> 
    {section name=r loop=$rows} 
    <tr bgcolor="{cycle values=#rowColors#}"> 
    <td> ....etc.... </td> 
    </tr> 
    {/section} 
</table> 
0

我看了一下Smarty文檔,特別是this頁面。

我沒有看到一個數組在這裏初始化的例子,我懷疑Smarty不支持在其配置文件中初始化數組,因爲配置文件似乎是簡單的鍵值存儲。您當然可以嘗試在配置文件中初始化數組,並讓我們知道它是否適用於您。

在其他Smarty文檔頁面上,它看起來像初始化數組的首選位置在.php頁面中,用於初始化值並在顯示模板之前將其加載到模板中。

+0

這似乎是真的。在那裏定義數組是不可能的。我試了一切..最後我用控制器分配數組(如你所建議的): php: $ smarty-> assign('list',array(1,2,3,4)); 產品。CONF [1] 客戶= BLA URL = BLA 價格= BLA 描述= BLA [2] 客戶= URL = 價格= 描述= [3] 客戶= URL = 價格= 描述= [4] 客戶= URL = 價格= 描述= TPL: {{foreach from = $ list item = current name = prod} {config_load file ='product.conf'section = $ current} {$ smarty.config.client} {$ smarty.config.url} { $ smarty.config.price} {$ smarty.config.description} {/ foreach} – quardas 2009-11-10 13:29:52

0

再次因爲格式化:

這似乎是對的。在那裏定義數組是不可能的。我什麼都試過..終於,我用分配陣列從控制器(如你所說):

PHP:

$smarty->assign('list', array(1,2,3,4)); 

product.conf

[1] 
client= 
url= 
price= 
description= 

[2] 
client= 
url= 
price= 
description= 

[3] 
client= 
url= 
price= 
description= 

[4] 
client= 
url= 
price= 
description= 

TPL:

{foreach from=$list item=current name=prod} 
{config_load file='product.conf' section=$current} 

{$smarty.config.client} 
{$smarty.config.url} 
{$smarty.config.price} 
{$smarty.config.description} 

{/foreach} 
0

有沒有什麼理由需要在smarty中完成這個需求?看起來,如果你只是在PHP中創建數組並傳遞給smarty變量,它可能會更有前途。這樣,如果你離開smarty,所​​有的數據仍然可以訪問,無需任何重新編碼。

相關問題