Re: [SMARTY] Re: passing an array to an included template

From: Date: Thu, 02 May 2002 20:55:57 +0000
Subject: Re: [SMARTY] Re: passing an array to an included template
References: 1 2 3 4 5 6 7 8  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-4004@lists.php.net to get a copy of this message
I already tried to explain my problem, but somehow it was a bit difficult to understand. :-) A very long explanation will follow now. I suggest you to have some coffee now. :P I have a small template for a navigation bar. I want to pass an array to this navigation-template within my main-template. Something like: {include file="navigation.tpl" navigation_names=array('Forums','Java','Entry 1234'} This should produce: <table> .... <a href="..">Home</a> >> <a href="..">Forums</a> >> <a href="..">Java</a> >> <a href="..">Entry 234</a> ... </table> I cannot declare the array $navigation_names in my PHP-file because I have only one PHP-file for the programming logic and a german and an english template for all output. I could get around this by a construct like: if($lang=="de") $smarty->assign("navigation_names",array( ... ) ); if($lang=="en") $smarty->assign("navigation_names",array( ... ) ); But this is exactly what I don't want: visible output in my php-file and assigning different vars for different languages. It's the strength of Smarty in comparision to other template engines to completely avoid this. The include statement above didn't work (at first), so I searched the documentation for a function to assign arrays within a template, but just found assign which is only useful for assigning strings. Being a bit disappointed I had a closer look at the source. I found out that Smarty automatically puts double-quotes around strings which are not enclosed in quotes. If I remove this feature a statement like above is possible, because the compiled template looks like: $this->_smarty_include("navigation.tpl", array( 'navigation_names'=> array('foo1','foo2','foo3') ) ); I think I don't have a single-case problem, so I have two solutions in mind: I/We/Someone could write a function which can be called this way: {assign_array var="foo" value1="bar1" value2="bar2" value3="bar3"} The other solution would be to change the behaviour of _parse_attrs in Smarty_Compiler.class.php. One way would be to disable the feature of automatically setting quotes (like I've done). The other way would be to disable the feature only when the value $token starts with the keyword "array". This way backward-compatibility-problems (ugly word) could be avoided. I hope you're still awake and can tell me your opinion. Jens PS: Bye the way it has to be "double_quotes" instead of "single_quotes" in Smarty_Compiler.class.php in line 1056 in the Smarty 2.1.0. release. > I knew I did that wrong when I sent it ;-) Without breaking Smarty > syntax, you'd have to do something like this: > > {assign_array var="foo" values="bar1:bar2:bar3"} > > Of course then you have an issue where you cannot have colons or double > quotes in your strings. Maybe we should take a step back. Exactly why do > you need to assign arrays from within the template? Maybe there is a > better way to solve your problem. > > Jens Lehmann wrote: > > Is it really good to have several "value"-keys? This imho causes > > problems because the values are not be accessible via the > > $params-array. How can I get around this or do you have > > an other solution? > > > > Jens > >

« previous php.smarty.general (#4004) next »