Page 1 of 1
2.0.1.1: Problem with template variables
Posted: Thu Oct 22, 2015 3:24 pm
by pschoenb
Hello,
since my site depends on it, I am trying to get the Babel module working with 2.0. Most issues are fixed. Amongst other things, I am retrieving the CMSMS class with
and I am retrieving smarty with
There is one weird thing, though: Some template varables set in the module are not visible when calling
For example, if I define a variable for test purposes in the module like this,
the variable is not included in the template variables.
Why?
Re: 2.0.1.1: Problem with template variables
Posted: Thu Oct 22, 2015 4:15 pm
by calguy1000
There is one weird thing, though: Some template varables set in the module are not visible when calling
This has been explained before (just yesterday in another thread... but I will do it one last time).
You are using the wrong $smarty template object.
CMSMS 2.0 as previously advertised takes advantage of the fact that you can create a new smarty template object inside of another one... and variables are not automatically copied from a child to a parent. (Though variables are copied from a parent to a child when the child template is initially created.)
In module actions the $smarty that is provided in scope may (and probably does) represent a different smarty template object than the global smarty template object.
In your plugins, UDT's, and module actions you should not be using the global smarty object, AT ALL, anywhere. This is not necessarily new to CMSMS 2.0, the $smarty variable was in scope before in UDT's and module actions. In plugins it is the second parameter to the plugin function, so it may have a different name (either $tpl, $smarty, or $template) ... but the purpose is the same.
Re: 2.0.1.1: Problem with template variables
Posted: Thu Oct 22, 2015 9:36 pm
by otopanne
calguy1000 wrote:
You are using the wrong $smarty template object.
&
calguy1000 wrote:
In your plugins, UDT's, and module actions you should not be using the global smarty object, AT ALL, anywhere.
The above example by pschoenb
is working perfect in 1.12.x and
was working perfect up to the august 22 rc1 beta version of v2.0.
Then you posted this:
http://forum.cmsmadesimple.org/viewtopi ... 00#p323400
And since the august 23 rc1 beta version the var is not being passed to the page template anymore.
So, what would be the right way to get a variable from a module to a page?
Re: 2.0.1.1: Problem with template variables
Posted: Sun Oct 25, 2015 5:20 pm
by pschoenb
I have the same problem as otopanne.
Let's say, I have a snippet like this one:
Code: Select all
function retrieve_page_lang(){
if(function_exists('cmsms')){
$gCms = cmsms();
}else{
global $gCms;
}
$smarty =& $gCms->GetSmarty();
// retrieve root id
if(!$this->available_languages) $this->retrieve_languages();
if(!$this->available_languages) return false;
foreach($this->available_languages as $onelang){
if($onelang["root_id"] == $root_id){
$this->current_langcode = $onelang["langcode"];
$this->current_language = $onelang;
}
}
$smarty->assign("page_lang", $this->current_langcode);
$smarty->assign("current_language", $this->current_language);
}
What exactly needs to be changed in order to get the template variables to global scope?
Re: 2.0.1.1: Problem with template variables
Posted: Sun Oct 25, 2015 7:42 pm
by pschoenb
Looks like I figured it out. Basically, the trick is to use
(Much less often needed now)
and
instead of getting all this stuff from cmsms().
Is this the officially recommended way of implementing things?
Re: 2.0.1.1: Problem with template variables
Posted: Sun Oct 25, 2015 10:00 pm
by Jo Morg
pschoenb wrote:Let's say, I have a snippet like this one:(...)
That's not nearly enough info on what it is that you pretend to do. The code you pasted seems too old and transitional since it is testing for the existence of a function that exists in CMSMS since version 1.7.
There is no context on the use of the snippet, so chances are that even if using the correct API calls it probably would not work.
pschoenb wrote:Looks like I figured it out. Basically, the trick is to use
(Much less often needed now)
and
instead of getting all this stuff from cmsms().
Is this the officially recommended way of implementing things?
The short answer is
no!
All the above is deprecated and odds are it won't work.
When not available (outside of an action for instance) all those have to be instantiated from cmsms(). When already in scope you should not use
$smarty = cmsms()->GetSmarty(); since this will override the $smarty already available and holding the current valid template object. This means that inside the action handling method or action file, or UDT you should NEVER have to get $smarty from nowhere. The same is valid for a plugin (as we have already replied in a few other posts) as the calling arguments include the current template object usually in a variable named $smarty or $template.
Please use the search feature of the forum, and document as well as possible (even if by excess) what it is that you want to do, how and in what context.
PS: Basically I'm reiterating the above post by calguy1000, which already explains all this...
Re: 2.0.1.1: Problem with template variables
Posted: Mon Oct 26, 2015 2:44 pm
by pschoenb
In my derived class of CMSModule, I always got Smarty via cmsms()->GetSmarty() but then, no template variable were available in the template.
Re: 2.0.1.1: Problem with template variables
Posted: Mon Oct 26, 2015 4:06 pm
by pschoenb
Tried it again. In the module class, I consistently get cmsms() like this
Code: Select all
if(function_exists('cmsms')){ $gCms =& cmsms(); }else{ global $gCms; }
and Smarty like this
but then, template variables like
Code: Select all
$smarty->assign('page_lang',$this->current_langcode);
are no longer propagated to the template.
Why?
Re: 2.0.1.1: Problem with template variables
Posted: Mon Oct 26, 2015 4:24 pm
by Jo Morg
One last time: context?
There are two clear replies to this topic. However you keep on not giving any details on what are you trying to do....
Code: Select all
if(function_exists('cmsms')){ $gCms =& cmsms(); }else{ global $gCms; }
Probably won't work! But even if it did it's not the correct way (as explained...).
Again: if inside an action this is not needed (if it was correct which it isn't...)
If needed (outside an action) the correct code would be
or if you override the DoAction method or are calling a class method
from an action use
Code: Select all
$smarty = $this->GetActionTemplateObject()
But as we have already stated twice:
you should not need to use either to get $smarty inside an action.
Calling
$smarty = cmsms()->GetSmarty(); will most likely
not give you the template used in the current action.
Re: 2.0.1.1: Problem with template variables
Posted: Mon Oct 26, 2015 6:59 pm
by pschoenb
Ok, fixed it now. Thanks.