4

I want to create an array in smarty and do an append functionality in it! Like if I declare a variable in smarty template like {assign var=sizearr value=''} and then i want to append values to this in a loop, and i can access values like {sizearr.0}, how can i do that?

4 Answers 4

4

Use append. I'm not sure if this is also available in Smarty 2

{append var='sizearr' value='' index=0}

Sign up to request clarification or add additional context in comments.

Comments

3

In smarty3 yould also use a more php-like approach:

{$sizearr[] = 'your value'}

and either loop through the array like

{foreach $sizearr as $value}
  {$value@key}: {$value}
{/foreach}

or just hit a specific index:

{$sizearr[2]}

Comments

1

You can use this too:

{$sizearr[] = "Size value"}

Here you can see the full doc (Section Appending an array)

Comments

1

You can simply use the smarty built-in function append :

Now lets take this example:

{assign var="ages" value=[] }

{for $i=1 to 3}
   {append var="ages" value=$i }
{/for}

In the above example we didn't specify index parameter in the append function, so the value will be appended at the end of the ages array.

Hope this is helpful for everyone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.