[ENHANCEMENT] get_template_vars on steroids vs debug

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
Grumpy

[ENHANCEMENT] get_template_vars on steroids vs debug

Post by Grumpy »

Hi folks,

I'm new here so please forgive me if this has been covered elsewhere.
As a noob I noticed i was fairly reliant on the debug and get_template_vars tags to learn about the variables on each page.

The first issue i encountered was the "memory exhausted" message in the debug output whenever debug got to a large object like $gCms, $mod or $feu_smarty.

Not being one to accept defeat, I've implemented a custom version of get_template_vars that exposes more of the template variables' properties. (like array contents)

If you comment out the existing function : smarty_cms_function_get_template_vars
inside the plugins/function.get_template_vars.php
and add the following in it's place, you'll be able to see more info about complex variables used in your pages:

Code: Select all

function smarty_cms_function_get_template_vars($params, &$smarty)
{
	global $gCms;
	
	$tpl_vars = $gCms->smarty->get_template_vars();
	
	$str = '<div style="overflow: auto; width: 500px; height: 400px;"><pre>';
	//$str = print_r($tpl_vars, true);
	foreach( $tpl_vars as $key => $value )
	  {
	  
	    if( !is_object($value) )
       {
	       $str .= "$key = $value<br/>";
       }
       else
       {
		 if ($key == 'gCms') {}
		 else if ($key== 'mod') {}
		 else if ($key == 'feu_smarty') {}
		 else
		 {
			$value = print_r($value,true);
         $str .= "$key = $value<br/>";
		}
       }
	  }
	  
	 $str .= "</pre></div>";
	return $str;
}
I've also attached a copy of the entire function.get_template_vars.php file for easier installation. (just rename your existing one before uploading it!)

Cheers,

-Damo (aka Grumpy)
Attachments

[The extension txt has been deactivated and can no longer be displayed.]

Leonix

Re: [ENHANCEMENT] get_template_vars on steroids vs debug

Post by Leonix »

Google brought me to this page when i searched for more complex version of get_template_vars. Function posted above inspired me to write my own version with functionality similar to PHP print_r(). It helps me much with module template debugging.

Features:
- Shows up to 10 levels of nesting.
- Output similar to PHP print_r().
- Doesn't recurse into objects that descend from CmsObject, Smarty or CMSModule.
- Boolean values are shown as 'true' and 'false' instead of '1' and ''.
- Iterates objects using foreach. This should work fine in PHP5 but will probably fail in PHP4.

To start using it, replace function smarty_cms_function_get_template_vars() in
/plugins/function.get_template_vars.php
with the following code:

Code: Select all

function ___my_print_r($value, $level = 0)
{
	if ($level > 9)
	{	// Being paranoid
		return 'Too big level of nesting';
	}

	if (!is_array($value) && !is_object($value))
	{
		if ($value === true)
		{
			return 'true';
		}
		else if ($value === false)
		{
			return 'false';
		}
		return $value;
	}

	$br = "\n"; // line break with tabs
	for($i = 0; $i < $level; $i++)
	{
		$br .= "\t";
	}

	if (is_object($value))
	{
		// Skip huge CMSMS core objects
		$class = get_class($value);
		do
		{
			if(in_array($class, array('CmsObject', 'Smarty', 'CMSModule')))
			{
				return get_class($value)." Object (skipped as a descendant of $class)";
			}
		}
		while ( ( $class = get_parent_class($class)));
		$str = get_class($value).' Object'.$br.'{';
	}
	else
	{
		$str = 'Array'.$br.'(';
	}

	foreach($value as $key => $val)
	{
		$str .= $br."\t".$key.' => '.___my_print_r($val, $level + 1);
	}
	$str .= is_array($value) ? $br.')' : $br.'}';
	return $str;
}

function smarty_cms_function_get_template_vars($params, &$smarty)
{
	global $gCms;

	$tpl_vars = $gCms->smarty->get_template_vars();

	$str = '<div style="overflow: auto; width: 500px; height: 400px;"><pre>'."\n\n";
	$str .= ___my_print_r($tpl_vars);
	$str .= "\n\n</pre></div>";
	return $str;
}
Entire file /plugins/function.get_template_vars.php also attached.
Attachments

[The extension txt has been deactivated and can no longer be displayed.]

Post Reply

Return to “Tips and Tricks”