user3381787
user3381787

Reputation: 21

How to append to multidimensional array in a smarty template?

I can append to a single array using

{append var='name' value='Bob' index='first'}

However, if I have a multi-dimensional array such as:

$name[first][last] = ['this','array']

and I want to append another value to the array at $name[first][last] e.g. to make the array like this:

$name[first][last] = ['this','array','appended']

how can I do this in the smarty template?

Upvotes: 2

Views: 2481

Answers (2)

eithed
eithed

Reputation: 4320

You can do this without using append:

{$name[first][last][] = 'this'}
{$name[first][last][] = 'array'}
{$name[first][last][] = 'appended'}

I must highlight though - templates should be used for specific purpose: to display prepared data; having to do the above is a code smell

Upvotes: 2

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

I've tested many cases to try achieve it and I think it's not possible (in documentation there is also no info or example of multidimensional key or var)

You should also really think do you need it at all. Logic should be in PHP and role of Smarty is only displaying data not manipulating them

Upvotes: 0

Related Questions