Page 1 of 1
Set variable with global scope from Tag v1.12.1
Posted: Wed Oct 21, 2015 10:52 pm
by chrisbt
In CMSMS 1.12.1 I have a tag called pageVars that I use to set an array $pageVars with various elements that I need available with global scope.
How can I set the $pageVars variable in the tag from a child template so that the variable has a global scope.
I have tried both:
Code: Select all
$smarty->assign('pageVars', $newvar);
$smarty->assignGlobal('pageVars', $newvar);
and they don't work.
I cannot seem to call the tag from the parent template and child template and set the variable in a global scope.
Thanks
Re: Set variable with global scope from Tag v1.12.1
Posted: Wed Oct 21, 2015 11:15 pm
by Jo Morg
Although you don't give much info on how you use the UDT or where, I'm assuming that the data you need to use globally should be available on the top of the main template. I would try to use the tag on top of the template, right after the {process_pagedata} like this:
Code: Select all
{pageVars}{$pageVars=$pageVars scope=global}
This way {$pageVars} should be available on all the templates from this point on.
More examples:
http://www.cmscanbesimple.org/blog/smar ... e-examples
Re: Set variable with global scope from Tag v1.12.1
Posted: Thu Oct 22, 2015 10:58 am
by chrisbt
Thanks Jo. I am understanding the issue more (I think) but am still not closer to a good fix.
I believe the issue I have is that once the smarty var is set in the parent template, it cannot be updated from a child template. Both for a varaible or an array element. Here are some more details:
page template (simplified):
Code: Select all
{process_pagedata}
{pageVars}{$pageVars=$pageVars scope=global}
{$testVar='test data from page template top' scope=global}
<!DOCTYPE html>
<__html lang="en">
<head>
{metadata}
</head>
</__body>
{Products}
<__body>
Tag: pageVars (simplified):
Code: Select all
function smarty_cms_function_pageVars ($params, &$tmp) {
$gCms = cmsms();
$smarty = $gCms->GetSmarty();
$pageVars['pageTitle'] = 'Default Page Title';
$pageVars['pageDescription'] = 'Default Page Description';
$smarty->assign( 'pageVars', $pageVars );
}
Products template (simplified):
Code: Select all
{$pageVars.pageTitle=$entry->product_name scope=global}{* does NOT work *}
{$pageVars.pageDescription=$entry->details|strip_tags|strip|truncate:150 scope=global}{* does NOT work *}
{$pageVars['TEST']='test data3 from Products' scope=global}{* does NOT work *}
{$testVar='test data from Products' scope=global}{* does NOT work *}
{$pageVars2=$pageVars scope=global}{* DOES work *}
{$testVarProducts='test data from Products' scope=global}{* DOES work *}
{* Note: only the 2 variables NOT previously set become available in metadata *}
metadata (simplified):
Code: Select all
<title>{$pageVars.pageTitle}</title>
<meta name="description" content="{$pageVars.pageDescription}" />
<!-- $pageVars is: {$pageVars|print_r}
$pageVars2 is: {$pageVars2|print_r}
$testVar is: {$testVar}
$testVarProducts is: {$testVarProducts}
-->
I now have a work around by testing for pageVars2 and overwriting PageVars, but this doesn't seem ideal, and I don't understand why I can't update a variable using scope=global?
Thanks
Re: Set variable with global scope from Tag v1.12.1
Posted: Thu Oct 22, 2015 11:19 am
by Jo Morg
chrisbt wrote:
Tag: pageVars (simplified):
Code: Select all
function smarty_cms_function_pageVars ($params, &$tmp) {
$gCms = cmsms();
$smarty = $gCms->GetSmarty();
$pageVars['pageTitle'] = 'Default Page Title';
$pageVars['pageDescription'] = 'Default Page Description';
$smarty->assign( 'pageVars', $pageVars );
}
I originally thought you were talking about UDTs. It seems that you are talking about plugins...
You never need to get an instance of the template like that... Smarty passes the current template object to the plugin on the second parameter (in your case &$tmp)...
So the correct way to assign values to smarty vars would be:
Code: Select all
function smarty_cms_function_pageVars ($params, &$tmp) {
$pageVars['pageTitle'] = 'Default Page Title';
$pageVars['pageDescription'] = 'Default Page Description';
$tmp->assign( 'pageVars', $pageVars );
}
For clarity we tend to declare Smarty plugins like this:
Code: Select all
function smarty_cms_function_pageVars ($params, &$smarty) {
$pageVars['pageTitle'] = 'Default Page Title';
$pageVars['pageDescription'] = 'Default Page Description';
$smarty->assign( 'pageVars', $pageVars );
}
$smarty being the template object currently used when the tag was called...
Otherwise you are running the risk of assigning vars to a template object that is not the one being used in the context where you call the plugin.
HTH
PS: please keep in mind that a tag can be anything from a module call to a UDT call, to a smarty plugin, or even a function, object or class registered with Smarty from the module library or other code. Tags don't have scope, variables do.
Re: Set variable with global scope from Tag v1.12.1
Posted: Thu Oct 22, 2015 11:54 am
by chrisbt
Many thanks. Very helpful & much tidier now (that I know).
After tidying that up I still have the issue that, if a smarty var is set in the parent template, it cannot be updated from a child template.
Re: Set variable with global scope from Tag v1.12.1
Posted: Thu Oct 22, 2015 12:16 pm
by Jo Morg
chrisbt wrote:After tidying that up I still have the issue that, if a smarty var is set in the parent template, it cannot be updated from a child template.
It should, as long as it is assigned on a parent template with the global scope... you shouldn't even have to re-scope it on a child template to the global context as it is supposed to exist there already...
I would try to test it in a simpler template structure just to see if it is reproducible, otherwise it may be the result of the complexity of the template(s) and something that you may be missing messing up the expected result.
Re: Set variable with global scope from Tag v1.12.1
Posted: Thu Oct 22, 2015 12:20 pm
by chrisbt
Ok great thanks. I'll do exactly that. Many thanks for all your comments very helpful & informative.
