2

When creating a custom smarty php plugin method, is it possible to pass in a multidimensional array as one of the params?

perhaps something like

{function title="Hi" options=array('opt1', 'opt2', 'opt3')}

The above is a sequential array, naturally support for an associative array would be equally as grand.

I've been scouring the docs and forums for hours but, unfortunately, everything I've been able to find has said "no" but has also been 5 years old (or more)

Thanks.

1
  • 1
    Also, the example isn't multidimensional, so the title is a bit off but support for both would be grand. Commented Apr 17, 2012 at 4:58

1 Answer 1

1

In Smarty3 you can.

This examples uses PHP5.4 short array syntax, which replaces array() with []. Just use it like this:

{function title="Hi" options=['opt1' => ['one' => 1], 'opt2' => ['two' => 2] ]}

Another example: Assign to a variable and loop over it.

{$multidimension_array = ['opt1' => ['one' => 1], 'opt2' => ['two' => 2] ]}

{foreach $multidimension_array as $ak => $subarray}
    <p>Section "{$ak}":</p>
    <ol>
    {foreach $subarray as $k => $v}
        <li>{$k}: {$v}</li>
    {/foreach}
    </ol>
{/foreach}

Also, refer to the Smarty3 overview page where this syntax is exemplified.

Note: PHP 5.4 is not required. This syntax is emulated at Smarty level.

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

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.