2013-07-03 55 views
1

我正在用Smarty 3升級一個使用Smarty 2的應用程序,並且我想在模板中將一個對象分配給一個變量名稱。原始代碼:有沒有辦法將一個對象分配給Smarty 3中的變量?

{section name=articles loop=$list_article} 
    {assign var="article" value="`$list_article[articles]`"} 

    // now use many properties of the object $article... 
    <h2>{$article->title}</h2> 
    {$article->text} 
    ... 
{/section} 

但是這不適用於Smarty 3,似乎{assign}只能分配字符串。它正在與Smarty 2合作。是否有Smarty 3的替代語法?

回答

1

避免引號並直接轉到該值。

{section name=articles loop=$list_article} 
    {assign var="article" value=$list_article[articles]} 

    // now use many properties of the object $article... 
    <h2>{$article->title}</h2> 
    {$article->text} 
    ... 
{/section} 

但是,在這種情況下,你也可以使用foreach。

{foreach from=$list_article item=article} 
    // now use many properties of the object $article... 
    <h2>{$article->title}</h2> 
    {$article->text} 
    ... 
{/section} 
+0

太棒了!謝謝! –

相關問題