Smarty debugging

4 messages Options
Embed this post
Permalink
SaWeyy () Smarty debugging
Reply Threaded More More options
Print post
Permalink
Hi,

I wrote a simple function to get a decent Smarty debug output, I hope someone can use it (maybe put it on the wiki)

function logSmarty(){
        $firephp = FirePHP::getInstance(true);
        
        $firephp->group('Smarty Debug Output');
        
        /*Log template files*/
        $firephp->group('included templates & config files (load time in seconds)');
        foreach($this->_smarty_debug_info as $tml){
            $msg = str_repeat('--', $tml['depth']);
            $msg .= ($tml['depth'] != 0) ? '>' : '';
            $msg .= $tml['filename'] . ' (' . substr($tml['exec_time'], 0, 7) . 's)'; 
            $firephp->log($msg);
        }
        $firephp->groupEnd();//end group 'included templates &...'
        
        /*Log assigned template variables*/
        $firephp->group('assigned template variables');
        foreach($this->_tpl_vars['_debug_keys'] as $key => $value){
            $firephp->log($this->_tpl_vars['_debug_vals'][$key], '{$' . $value . '}');
        }
        $firephp->groupEnd();//end group 'assigned template variables'
        
        /*Log assigned config file variables (outer template scope)*/
        $firephp->group('assigned config file variables (outer template scope)');
        foreach($this->_tpl_vars['_debug_config_keys'] as $key => $value){
            $firephp->log($this->_tpl_vars['_debug_config_vals'][$key], '{#' . $value . '#}');
        }
        $firephp->groupEnd();//end group 'assigned config file variables (outer template scope)'
        
        
        $firephp->groupEnd();//end group 'Smarty Debug Output'
}



PS: Thanks for this great tool
Christoph Dorn () Re: Smarty debugging
Reply Threaded More More options
Print post
Permalink
Thanks for contributing this. I am sure others can benefit from it.

Can you post some instructions on how to use this function in your app (and with smarty) that I can include in the wiki.
SaWeyy () Re: Smarty debugging
Reply Threaded More More options
Print post
Permalink
Ok, so I changed it a bit for easier integration:
This class extends the Smarty class and adds a new function to it

--------------------------------------------------------
<?php

// load Smarty library
require ( 'includes/Smarty/Smarty.class.php' );

class Template extends Smarty{
    
    function Template(){
        $this->Smarty();
        
        $this->template_dir = './templates';
        $this->compile_dir = './templates/compiled';
        $this->config_dir = './templates/config';
        $this->cache_dir = './cache';
        
        //$this->caching = false;
    }
    
    function smartyDebugFirePHP(){
        //get required debug variables
        $assigned_vars = $this->_tpl_vars;
        ksort($assigned_vars);
        
        $config_vars = array();
        if (@is_array($this->_config[0])) {
            $config_vars = $this->_config[0];
            ksort($config_vars);
        }        
        
        $firephp = FirePHP::getInstance(true);
        
        $firephp->group('Smarty Debug Output');    
            /*Log template files*/
            $firephp->group('included templates & config files (load time in seconds)');
                foreach($this->_smarty_debug_info as $tml){
                    $msg = str_repeat('--', $tml['depth']);
                    $msg .= ($tml['depth'] != 0) ? '>' : '';
                    $msg .= $tml['filename'] . ' (' . substr($tml['exec_time'], 0, 7) . 's)'; 
                    $firephp->log($msg);
                }
            $firephp->groupEnd();//end group 'included templates &...'
            
            /*Log assigned template variables*/
            $firephp->group('assigned template variables');
                foreach($assigned_vars as $key => $value){
                    $firephp->log($value, '{$' . $key . '}');
                }
            $firephp->groupEnd();//end group 'assigned template variables'
            
            /*Log assigned config file variables (outer template scope)*/
            $firephp->group('assigned config file variables (outer template scope)');
                /*Check if there is something in the config*/
                if(!empty($config_vars)){
                    foreach($config_vars as $key => $value){
                        $firephp->log($value, '{#' . $key . '#}');
                    }
                }else{
                    $firephp->log("No configuration values available");
                }
            $firephp->groupEnd();//end group 'assigned config file variables (outer template scope)'
        
        
        $firephp->groupEnd();//end group 'Smarty Debug Output'
    }

}

--------------------------------------------------------



Use this class instead of Smarty to consume your templates like below:

--------------------------------------------------------
<?php
$tpl = new Template();

/*Do your Smarty things below*/
/*...*/
/*At the end of your application, call the log function*/

$tpl->smartyDebugFirePHP();

--------------------------------------------------------


Another thing you could do is replace the Smarty debugger (internals/core.display_debug_console.php)
This way, you would simply replace the debugger window of Smarty with the FirePHP output.
I would not recommend this because you are modifying core code of Smarty, and upgrading can remove your code.
If you want to do this, replace the content of the file with the code below:
--------------------------------------------------------
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty debug_console function plugin
 *
 * Type:     core<br>
 * Name:     display_debug_console<br>
 * Purpose:  display the javascript debug console window
 * @param array Format: null
 * @param Smarty
 */
function smarty_core_display_debug_console($params, &$smarty)
{
    //get required debug variables
    $assigned_vars = $smarty->_tpl_vars;
    ksort($assigned_vars);
   
    $config_vars = array();
    if(@is_array($smarty->_config[0])){
        $config_vars = $smarty->_config[0];
        ksort($config_vars);
    }
   
    $firephp = FirePHP::getInstance(true);
   
    $firephp->group('Smarty Debug Output');
    /*Log template files*/
    $firephp->group('included templates & config files (load time in seconds)');
    foreach( $smarty->_smarty_debug_info as $tml ){
        $msg = str_repeat('--', $tml['depth']);
        $msg .= ( $tml['depth'] != 0 ) ? '>' : '';
        $msg .= $tml['filename'] . ' (' . substr($tml['exec_time'], 0, 7) . 's)';
        $firephp->log($msg);
    }
    $firephp->groupEnd(); //end group 'included templates &...'
   

    /*Log assigned template variables*/
    $firephp->group('assigned template variables');
    foreach( $assigned_vars as $key => $value ){
        $firephp->log($value, '{$' . $key . '}');
    }
    $firephp->groupEnd(); //end group 'assigned template variables'
   

    /*Log assigned config file variables (outer template scope)*/
    $firephp->group('assigned config file variables (outer template scope)');
    /*Check if there is something in the config*/
    if(!empty($config_vars)){
        foreach( $config_vars as $key => $value ){
            $firephp->log($value, '{#' . $key . '#}');
        }
    } else{
        $firephp->log("No configuration values available");
    }
    $firephp->groupEnd(); //end group 'assigned config file variables (outer template scope)'
   

    $firephp->groupEnd(); //end group 'Smarty Debug Output'
}

/* vim: set expandtab: */

?>
--------------------------------------------------------
(don't forget to enable debugging when using this method!)
Christoph Dorn () Re: Smarty debugging
Reply Threaded More More options
Print post
Permalink
In reply to this post by SaWeyy
Thanks for the code! I have updated the Smarty (http://www.firephp.org/Wiki/Libraries/Smarty) page.

Feel free to post code updates to this thread if available.