RE: Re[8]: [SMARTY] foreach/assign conflict? dropping last value

From: Date: Tue, 15 Oct 2002 16:24:55 +0000
Subject: RE: Re[8]: [SMARTY] foreach/assign conflict? dropping last value
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-6160@lists.php.net to get a copy of this message
It doesn't matter what sort of loop you use, you still can't combine template variables inside of double quotes... maybe write a plugin that would handle the combination, and the assign, might look something like this (see the plugin section of the smarty docs for more info): smarty_function_combine_assign($params, &smarty){ extract($params); $smarty->assign($var1.$var2, $value); } and you could call the function from smarty (in your foreach loop) like so: {combine_assign var1="order_" var2=$key value=$item} then you could avoid the {capture} call. -- Josh -----Original Message----- From: guido [mailto:sguido@rogers.com] Sent: Tuesday, October 15, 2002 12:13 PM To: Josh Johnson Cc: Smarty Subject: Re[8]: [SMARTY] foreach/assign conflict? dropping last value Hello Josh, good answer ;) is it possible to do this loop in a section call? I'm not sure how to do them with a $key->item function and even if it's possible. current code is: {foreach key=key item=item from=$post_vars} {capture name="order_key"}order_{$key}{/capture} {assign var=$smarty.capture.order_key value=$item} <input type="hidden" name="{$key}" value="{$item}"> {* FORM *} {/foreach} just wondering if using section & assign if the capture part is ness.. guido Tuesday, October 15, 2002, 12:05:11 PM, you wrote: JJ> I dunno! :) JJ> -- Josh JJ> -----Original Message----- JJ> From: guido [mailto:sguido@rogers.com] JJ> Sent: Tuesday, October 15, 2002 12:00 PM JJ> To: Josh Johnson JJ> Cc: Smarty JJ> Subject: Re[6]: [SMARTY] foreach/assign conflict? dropping last value JJ> Hello Josh, JJ> ok that works :) JJ> now my question is why does the capture work and the plain assign JJ> variable leaves out the last for value? JJ> Tuesday, October 15, 2002, 11:39:38 AM, you wrote: JJ>> The _isbn was just an example of adding a value to the end of JJ> variable JJ>> output... I was trying to be funny, since so many people write JJ> example JJ>> code for selling books (ISBN is the universal identifier for JJ>> publications), and I was using an array of socks :) JJ>> Bascially, the idea is to assign a separate variable that creates JJ> the JJ>> string you want to use as a variable name, or a variable value. You JJ>> don't have access to smarty variables inside of strings within JJ>> attributes, so you can't concat strings in the usual php way... JJ>> So your original code has two errors in it: JJ>> {foreach key=key item=item from=$post_vars} JJ>> {assign var="order_$key" value="$item"} <-- right here! JJ>> <input type="hidden" name="{$key}" JJ>> value="{$item}"> {* FORM *} JJ>> {/foreach} JJ>> if you change that line to these lines: JJ>> {capture name="order_key"}order_{$key}{/capture} JJ>> {assign var=$smarty.capture.order_key value=$item} JJ>> then it should work like you'd expect. Notice how since I'm JJ> referencing JJ>> template variables, I didn't put quotes around them in the {assign} JJ>> call. JJ>> -- Josh JJ>> -----Original Message----- JJ>> From: guido [mailto:sguido@rogers.com] JJ>> Sent: Tuesday, October 15, 2002 11:10 AM JJ>> To: Josh Johnson JJ>> Cc: 'Smarty' JJ>> Subject: Re[4]: [SMARTY] foreach/assign conflict? dropping last JJ> value JJ>> Hello Josh, JJ>> this doesn't work for me .. it assigns the key name and not the JJ> result JJ>> value. although it does isolate the correct last item. JJ>> questions .. what does the _isbn mean? anything? JJ>> and why do all the items work in the assign (without the capture) JJ>> except the last one? JJ>>> Ok, you can't use a smarty variable inside of a string like that... JJ>> so JJ>>> your assign call just isn't working... You'll need to assemble the JJ>>> variable name some other way, and it should work... I've used JJ>> {capture} JJ>>> in the past with no problems: JJ>>> {foreach item=sock from=$socks key=key} JJ>>> {capture name=sock_isbn}sock_{$key}{/capture} JJ>>> {assign var=$smarty.capture.sock_isbn value=$sock} JJ>>> {/foreach} JJ>>> That should work the way you intend... I'm sure there are other JJ>> ways, JJ>>> I'm not sure how efficient the capture function is since it uses JJ> the JJ>>> output buffering functions of php, but like I said, I haven't had JJ>> any JJ>>> problems. JJ>>> -- Josh JJ>>> -----Original Message----- JJ>>> From: guido [mailto:sguido@rogers.com] JJ>>> Sent: Tuesday, October 15, 2002 10:30 AM JJ>>> To: Josh Johnson JJ>>> Cc: 'Smarty' JJ>>> Subject: Re[2]: [SMARTY] foreach/assign conflict? dropping last JJ>> value JJ>>>> A few questions: JJ>>>> Why aren't you using $smarty.post? JJ>>> Because the data isn't a pure post, it's coming from another module JJ>>> that's setting the loop data to $post_vars array. JJ>>>> When are you trying to access that assigned variable? (the JJ>>> order_$key JJ>>>> one)? Inside of the foreach loop, or outside? JJ>>> The assign is going on inside the loop as shown. JJ>>> And access is going on outside the loop, but it also doesn't work JJ>>> inside the loop either. JJ>>>> -- Josh JJ>>>> -----Original Message----- JJ>>>> From: guido [mailto:sguido@rogers.com] JJ>>>> Sent: Tuesday, October 15, 2002 12:03 AM JJ>>>> To: Smarty JJ>>>> Cc: Martin Kjeldsen JJ>>>> Subject: [SMARTY] foreach/assign conflict? dropping last value JJ>>>> ok this is a weird one ... JJ>>>> what I'm doing is taking a HTTP_POST_VARS and looping through JJ> them, JJ>>>> setting them as hidden form fields and then setting them as JJ>>> variables: JJ>>>> {foreach key=key item=item from=$post_vars} JJ>>>> {assign var="order_$key" value="$item"} JJ>>>> <input type="hidden" name="{$key}" JJ>>>> value="{$item}"> {* FORM *} JJ>>>> {/foreach} JJ>>>> Now here's the weird part .. all of the input/hidden fields show JJ>> up, JJ>>>> but not the assigned variables. The last one drops off and isn't JJ>>>> available. JJ>>>> if I add in another: JJ>>>> {assign var="order_$key" value="$item"} JJ>>>> after the input, it works. JJ>>>> Martin Kjeldsen seem to have a similar problem: JJ>>>> JJ>>> JJ> JJ> http://marc.theaimsgroup.com/?l=smarty-general&m=101786148311641&w=2 JJ>>>> but there was no resolution there... JJ>>>> any ideas? JJ>>>> -- JJ>>>> Best regards, JJ>>>> guido JJ>>>> mailto:sguido@rogers.com JJ>>>> -- JJ>>>> Smarty General Mailing List (http://smarty.php.net/) JJ>>>> To unsubscribe, visit: JJ>>>> http://www.php.net/unsub.php JJ>>> -- JJ>>> Best regards, JJ>>> guido JJ>>> mailto:sguido@rogers.com JJ>> -- JJ>> Best regards, JJ>> guido JJ>> mailto:sguido@rogers.com -- Best regards, guido mailto:sguido@rogers.com

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