Re: [SMARTY] plugin & assign weirdness
| From: | messju mohr | Date: | Mon, 16 Feb 2004 23:11:40 +0000 |
| Subject: | Re: [SMARTY] plugin & assign weirdness | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-10966@lists.php.net to get a copy of this message | ||
On Mon, Feb 16, 2004 at 06:29:00PM +0000, david5@student-kmt.hku.nl wrote:
> Hello,
>
>
>
> I hope you gurus out there still have some patience left for me.
> I found out some more stuff about my problem and I think the mystery
> is partly solved.
>
>
> >From a (function) plugin, assign() does not assign vars back to the
> template. I found out that this is only in the case that the plugin is
> called from a nested template (called by an 'include' in the main
> template). As long as I call the plugin directly from my main
> template, everything works fine. I think this is weird as I *am* able
> to succesfully do a get_template_vars() from wherever.
>
> I want to be able to call the plugin from any (sub)template, does
> anybody have any ideas or workarounds? Could I write back to the
> template directly, with $smarty._tpl or something?
The scope of changes to template-variables is local to the included
template. The template-variables are restored when the included
template is done. Maybe this should be stated more clear on the
manual-page for {include}.
There is no really easy way around that.
One trick (somebody suggested some time ago, but i forgot who and
where, sorry): If you know in advance which variables you want to
handle as globals (= which ones should be changeable by included
templates) is: Assign them with assign_by_ref() from php. Then they
are handled as references in the template and the included-template
assigns really the same variable as the including one uses.
If you don't have values to assign, you can simply do somthing like:
$smarty->assign_by_ref('var1', $dummy = null);
unset($dummy);
$smarty->assign_by_ref('var2', $dummy = null);
unset($dummy);
/* {$var1} and {$var2} should be changeable by included tpls now */
Another way would be if you don't use {include} but write your own
plugin that uses $smarty->fetch() to retrieve an included template.
> thanx, greetings
> --David Spreekmeester
>
>
>
>
> DS> Hi,
>
> DS> Thanks for your replies.
>
> DS> I'm afraid I didn't find any answer yet though, as I do use a
> DS> reference to the smarty object. In fact, this is the code I brought it
> DS> down to (for debugging purposes):
>
> DS> function smarty_function_formbutton($params,&$smarty){
> DS> $smarty->assign
> ('thisVar','neverAppearsInOutputOrDebugConsole');
> DS> return 'rrroar';
> DS> }
>
> DS> Also, I tested it on my Windows PHPDev development environment as well
> DS> as on a Linux production environment, and the same error occurs:
> DS> 'rrroar' comes through, but 'thisVar' never does.
> DS> I must be overlooking something... Placing a correctly-named plugin in
> DS> the plugin directory is all I need to do, right?
>
>
> DS> see ya
> DS> --David Spreekmeester
>
>
> mm>> My guess is you don't pass the $smarty object by reference:
>
> mm>> you declared it (wrong):
> mm>> function smarty_function_plugin($params, $smarty)
>
> mm>> instead of (correct):
> mm>> function smarty_function_plugin($params, &$smarty)
>
> mm>> if so, you are working on a copy of $smarty in your function.
>
> mm>> greetings
> mm>> messju
>
>
>
> >>> Hello,
> >>>
> >>> Writing a function plugin for Smarty, I keep on running in to the
> >>> problem of using assign() to set a template variable. This doesn't
> >>> seem to work as it should if I understand the manual correctly.
> >>>
> >>> Returning a value from the function works fine, but when I try to
> >>> assign a variable to the template ($smarty->assign), nothing comes
> >>> through in the output nore in the debug console.
> >>> I can however *retrieve* a template variable (by get_template_vars())
> >>> and return it from the function. Because I can reach the template from
> >>> within the plugin, but not the other way around (as it seems), I
> >>> kind of excluded the possibility of a scope issue.
>
>
> >>> So does it have to do with order in the parsing process or...?
> >>> I can't find much information on a problem like this.
> >>>
> >>>
> >>> does this make sense to anyone?
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php