Re: [SMARTY] 2.4.1: {assign} in tenmplates with arrays
| From: | Ferdinand Beyer | Date: | Fri, 07 Feb 2003 16:58:48 +0000 |
| Subject: | Re: [SMARTY] 2.4.1: {assign} in tenmplates with arrays | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-7572@lists.php.net to get a copy of this message | ||
On 7 Feb 2003 at 15:05, Mark Rogers wrote:
> I'm running current CVS (as of this morning). I'm fairly sure this used to
> work pre-2.4 but correct me if I'm wrong:
I'm sure exactly the same could did never work with Smarty!
>
> $smarty->assign('test', array('name' => 'Mark'));
>
> ... and in template:
>
> {assign var=subject value="Hello, $test.name, from Joe"}
> {$subject}
Please note that currently there is little support for variables in
strings. Actually Smarty does only let PHP do the dirty work (like
always :-)).
In detail, Smarty doesn't look for variables in strings - it simply leaves
them untouched.
> .. currently prints
> Hello, Array.name, from Joe
>
> .. where I obviously wanted
> Hello, Mark, from Joe
>
Your code gets compiled to
echo "Hello, $test.name, from Joe";
PHP parses this line and looks for a variable called $test. Smarty
extracts the template vars to the function's namespace and therefore
$test is found - it's the array you assigned above.
But since PHP doesn't accept the Array.key syntax, it only converts
$test to the string "Array" to indicate that you tried to echo() an array.
The result is "Array.name".
> I can work around this with
> {assign var=temp value=$test.name}
> {assign var=subject value="Hello, $temp, from Joe"}
> {$subject}
This should also work (in this case):
{assign var=subject value="Hello, $test[name] from Joe"}
Another alternative is using {capture}
> .. but you try explaining that to a template designer :-)
I agree, this solution is not perfect yet. But perhaps some day there
is a better one...
HTH
--
Ferdinand Beyer
<fbeyer@clickhand.de>