broken scope of variables in foreach with assign

From: Date: Fri, 01 Nov 2002 22:45:43 +0000
Subject: broken scope of variables in foreach with assign
Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-314@lists.php.net to get a copy of this message
I wanted to construct a URL out of some parameters passed into a template. I tried what seemed most obvious to me: {assign name="url" value="page.php?"} {foreach from=$params key=key value=value} {assign name="url" value="$url&$key=$value"} {/foreach} That did not work, however. After much examination, I discovered it was because the $key and $value variables were not in scope for the assign until the NEXT iteration. So with an array(0 => 'a', 1 => 'b', 2 => 'c'), I would end up with a result like "page.php?&=&0=a&1=b". The last pair would be lost becuse there was no assign after the final values were brought into scope in the generated php script. I looked through the Smarty source and it does not look like an easy fix, but it also does not look really hard either. For now, I have two options. I can either come up with a custom function that generates URLs (which I did) or I can do something like the following: {capture name="target"}user.php?{foreach from=$searchParameters key=k item=v}&{$k}={$v}{/foreach}{/capture}{assign var="target" value=$smarty.capture.target} Neither of those is a perfect solution, however, as the scoping problem has broader implications. Was this behavior by choice, or is it an overlooked bug? Or am I just missing something?

« previous php.smarty.dev (#314) next »