Question

I am upgrading an application using Smarty 2 with Smarty 3 and I want to assign in the template an object to a variable name. The original code:

{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}

But this doesn't work with Smarty 3, it seems that {assign} can assign only string. It was working with Smarty 2. Is there a replacement syntax for Smarty 3?

Was it helpful?

Solution

Avoid the quotes and go straight to the value.

{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}

However, in this case, you can also use foreach.

{foreach from=$list_article item=article}
  // now use many properties of the object $article...
  <h2>{$article->title}</h2>
  {$article->text}
  ...
{/section}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top