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

From: Date: Thu, 02 May 2002 21:04:14 +0000
Subject: RE: [SMARTY] Re: passing an array to an included template
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-4007@lists.php.net to get a copy of this message
I recently wrote a plugin which deals with variable values, eg. {call func="foo" arg1="one" arg2="three" .... } One way to deal with an open ended list of arguments is to test to see if variable exists in a while loop with isset() where the variable has an incrementing number at the end, eg: arg1, arg2, arg3.. Eg: function smarty_function_call($params, &$smarty) { /* SNIP */ $i = 1; $args = array(); while( isset($params["arg$i"]) ){ array_push($args,$params["arg$i"]); $i++; } /* SNIP */ } That said, I think it makes no sense to have a plugin which assigns values to an array, since any plugin would operate in a predeterministic way; any array you assign through a plugin will always have the same number of elements, so why make it an array? Array assignment & population is definitely something that should happen outside the template... The flexibility of a Smarty often makes it tempting to write a plugin every time you can't do something with out-of-the-box smarty functionality, but people really need to be careful that they're not just creating some new variant of {PHP}. Smarty and templating engines in general are meant to separate business logic from the 'view'. The fact that you can't do complicated PHP-esque things in a smarty template shouldn't be viewed as a limitation that should be hacked around with a plugin. Instead, people need to remember that this is the POINT of a templating tool. IMHO... ;) Anyhow, I just wanted to get that off my chest since I've been noticing a lot of odd feature requests, and Monte and Andrei have been doing a great job at keeping Smarty a templating API and not PHP-based hypertext processor. -mark > -----Original Message----- > From: Monte Ohrt [mailto:monte@ispi.net] > Sent: Thursday, May 02, 2002 4:11 PM > To: Jens Lehmann > Cc: smarty-general@lists.php.net > Subject: Re: [SMARTY] Re: passing an array to an included template > > 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 > > > > > >>You'll have to make a custom function. > >> > >>{assign_array var="foo" value="bar1" value="bar2" > >>value="bar3"} > >> > >> > >> > >>Jens Lehmann wrote: > >> > >>>You almost got it. :-) > >>> > >>>The problem is to assign an array in the template (not in the > >> > > PHP-file!). > > > >>>There is a function "assign" for this purpose. > >>>You can look it up in the documentation under II 8. Custom Functions. > >>> > >>>The example in the documentation is: > >>> > >>>{assign var="name" value="Bob"} > >>> > >>>var and name are strings, that's why I see no possibility to assign an > >> > > array > > > >>>in > >>>a template. Imho this should be possible. > >>> > >>>If I want to do something like > >>> > >>>{assign var="name" value=array("test1","test2") } > >>> > >>>I won't get the expected result, because Smarty treats > >>>array("test1","test2") as > >>>an unquoted string (remember the assign-function expects a string) and > >>>quotes > >>>it. I removed Smarty's behaviour of quoting unquoted strings and > >> > > everything > > > >>>works > >>>fine. > >>> > >>>My question was if there's any possibility to assign an array in a > >> > > template? > > > >>>If no, I wanted to know if it could cause errors to remove Smarty's > >>>behaviour > >>>of quoting unquoted strings. (again you may look it up in > >>>Smarty_Compiler.class.php > >>>the lines around 1060 in Smarty 2.1.0) > >>> > >>>Anyways it could be a better solution to make assign accept arrays by > >>>default, or not? > >>> > >>>An example: > >>> > >>>{assign var="name" values=("test1","test2") } > >>> > >>>Hopefully, you can understand my thoughts now. > >>> > >>>Jens > >>> > >>> > >> > > > > > > > > > > > > -- > Monte Ohrt <monte@ispi.net> > Director of Technology, ispi Inc. > > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

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