2.0.1.1: Problem with template variables

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
pschoenb
Forum Members
Forum Members
Posts: 92
Joined: Sun Jul 15, 2007 1:18 pm

2.0.1.1: Problem with template variables

Post 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

Code: Select all

$gCms = cmsms();
and I am retrieving smarty with

Code: Select all

$smarty =& $gCms->GetSmarty();
There is one weird thing, though: Some template varables set in the module are not visible when calling

Code: Select all

{get_template_vars}
For example, if I define a variable for test purposes in the module like this,

Code: Select all

$smarty->assign('BOO', "BANG");
the variable is not included in the template variables.

Why?
Regards,
Patrick
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: 2.0.1.1: Problem with template variables

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
otopanne
Forum Members
Forum Members
Posts: 20
Joined: Tue Sep 15, 2015 4:43 pm

Re: 2.0.1.1: Problem with template variables

Post 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?
pschoenb
Forum Members
Forum Members
Posts: 92
Joined: Sun Jul 15, 2007 1:18 pm

Re: 2.0.1.1: Problem with template variables

Post 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?
Regards,
Patrick
pschoenb
Forum Members
Forum Members
Posts: 92
Joined: Sun Jul 15, 2007 1:18 pm

Re: 2.0.1.1: Problem with template variables

Post by pschoenb »

Looks like I figured it out. Basically, the trick is to use

Code: Select all

$this->cms
(Much less often needed now)

Code: Select all

$this->smarty

Code: Select all

$this->db
and

Code: Select all

$this->config
instead of getting all this stuff from cmsms().

Is this the officially recommended way of implementing things?
Regards,
Patrick
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1973
Joined: Mon Jan 29, 2007 4:47 pm

Re: 2.0.1.1: Problem with template variables

Post 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

Code: Select all

$this->cms
(Much less often needed now)

Code: Select all

$this->smarty

Code: Select all

$this->db
and

Code: Select all

$this->config
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...
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
pschoenb
Forum Members
Forum Members
Posts: 92
Joined: Sun Jul 15, 2007 1:18 pm

Re: 2.0.1.1: Problem with template variables

Post 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.
Regards,
Patrick
pschoenb
Forum Members
Forum Members
Posts: 92
Joined: Sun Jul 15, 2007 1:18 pm

Re: 2.0.1.1: Problem with template variables

Post 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

Code: Select all

$smarty =& $gCms->GetSmarty();
but then, template variables like

Code: Select all

$smarty->assign('page_lang',$this->current_langcode);
are no longer propagated to the template.

Why?
Regards,
Patrick
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1973
Joined: Mon Jan 29, 2007 4:47 pm

Re: 2.0.1.1: Problem with template variables

Post 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...).

Code: Select all

$smarty =& $gCms->GetSmarty();
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

Code: Select all

$smarty = cmsms()->GetSmarty();
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.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
pschoenb
Forum Members
Forum Members
Posts: 92
Joined: Sun Jul 15, 2007 1:18 pm

Re: 2.0.1.1: Problem with template variables

Post by pschoenb »

Ok, fixed it now. Thanks.
Regards,
Patrick
Post Reply

Return to “The Lounge”