Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

HowTo use Debug to avoid using echo when develop. your code

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
Isidor128
Smarty Regular


Joined: 27 Jul 2004
Posts: 35
Location: France

PostPosted: Thu Jul 29, 2004 8:16 pm    Post subject: HowTo use Debug to avoid using echo when develop. your code Reply with quote

Hi,

I've been using Smarty for almost 2 years for a 14000 user community.
The site was first coded with xtemplate but when I'd to take it over I wasn't comfortable with this template solution.

I investigate and decided to switch to Smarty, port from Xtlp was not complex and could be done very quickly (but without taking advantage of all Smarty capabilities)

On point I left over on this time was to do something convenient for displaying variables and development comments when testing the code.


Today I'm in a process to re-engineer the site and my first action was to find a clean way to handle Debug outputs.
BTW Smarty offer a very nice Debug Console so I decided to tweak it to display not only the Smarty Debug Info but my own $vars and comments.

As I'm happy with it, I wanted to share it with you.

First (and that's nothing to do with Smarty Smile ) we could setup DEBUG parameters to initiate or not Debugging output


[php:1:66a65f65f4]<?php
//****************************
// Activation Debuging
//****************************

// Set 1 to Initiate DEBUG actions
define("DEBUG", "1");

// Define there your own IP so only you will see the DEBUG output
define("IPDEBUG", "82.228.128.40"); #MyIP

// IPCNX refers to the Visitor IP
define("IPCNX",$REMOTE_ADDR);

?>[/php:1:66a65f65f4]

Now we have to define a Syntax:

For example function name could be: uDebug($var, $val)

and whe could decide that :
    If $var start with '$', $var is considered to be a variable or an array (As SmartyDebug do)
    If $var do not start with '$', $var is considered to be a text (i.e. will not be truncated As SmartyDebug do)
    if $var equal 'title', $var is considered to be Title ($var will be displayed on the full width of the table)


and the Function could be written

[php:1:66a65f65f4]<?php
function uDebug($name, $val='') {

if (!$val) $val='unset';

if (DEBUG && IPDEBUG==IPCNX) {
$this->append("UserDebugKey",$name);
$this->append("UserDebug",$val);
}
}
?>[/php:1:66a65f65f4]
Easy Isn't it ?

For sure the function need to be linked to Smarty so this have to be done this way

[php:1:66a65f65f4]<?php
class Smarty_Mine extends Smarty {

function Smarty_Mine() {

// Class Constructor. These automatically get set with each new instance.
$this->Smarty();

$this->template_dir = SCRIPT_DIR;
$this->compile_dir = SMARTYCOMPILE_DIR;
$this->config_dir = SMARTYCONFIG_DIR;
$this->cache_dir = SMARTYCACHE_DIR;
$this->compile_check = true;
$this->debug_tpl = SMARTY_DIR.'debugmine.tpl';
$this->debugging = true;
$this->plugins_dir = array('plugins_mine','plugins_validate','plugins');

$this->assign('app_name','Mine');
}

// And the Debug Thing

function uDebug($name, $val='') {

if (!$val) $val='unset';

if (DEBUG && IPDEBUG==IPCNX) {
$this->append("UserDebugKey",$name);
$this->append("UserDebug",$val);
}
}

}?>[/php:1:66a65f65f4]

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

Now we are ready to Hack the debug.tpl

I will not show the full debug.tpl but only the section where I've nested my code:

[php:1:66a65f65f4]....
_smarty_console = window.open("",title.value,"width=680,height=600,resizable,scrollbars=yes");
_smarty_console.document.write("<HTML><TITLE>Smarty & User Debug Console_"+self.name+"</TITLE><BODY bgcolor=#ffffff>");
_smarty_console.document.write("<table border=0 width=100%>");
_smarty_console.document.write("<tr bgcolor=#cccccc><th colspan=2>Smarty & User Debug Console</th></tr>");

_smarty_console.document.write("<tr bgcolor=#cccccc><th colspan=2><b>User Debug Console</b></th></tr>");

{section name=vars loop=$UserDebug}
{if $UserDebugKey[vars] eq 'title'}
_smarty_console.document.write("<tr bgcolor=#cccccc><td colspan=2><font color=darkblue><b>{$UserDebug[vars]}</b></font></td></tr>");
{* elseif preg_match('~^\$~',$UserDebugKey[vars]) *}
{elseif substr($UserDebugKey[vars],0,1) eq '$'}
_smarty_console.document.write("<tr bgcolor={if %vars.index% is even}#eeeeee{else}#fafafa{/if}><td valign=top><tt><font color=blue>{ldelim}{$UserDebugKey[vars]}{rdelim}</font></tt></td><td nowrap><tt><font color=green>{$UserDebug[vars]|@debug_print_var|escape:javascript}</font></tt></td></tr>");
{else}
_smarty_console.document.write("<tr bgcolor={if %vars.index% is even}#eeeeee{else}#fafafa{/if}><td valign=top><tt><font color=blue>[<b>{$UserDebugKey[vars]}</b>]</font></tt></td><td nowrap><tt><font color=green>{$UserDebug[vars]|wordwrap:50:"<br />"|escape:javascript}</font></tt></td></tr>");
{/if}
{sectionelse}
_smarty_console.document.write("<tr bgcolor=#eeeeee><td colspan=2><tt><i>no User Debug Information</i></tt></td></tr>");
{/section}

_smarty_console.document.write("<tr bgcolor=#cccccc><th colspan=2><b>Smarty Debug Console</b></th></tr>");
....[/php:1:66a65f65f4]

I invented nothing, just adding a few line to swhat was there....

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

Now we're done so we could use it where we want...

[php:1:66a65f65f4]<?php
...
$smarty->uDebug('title',"My Own debug section");
$smarty->uDebug('title',"* John Doe was there");
$smarty->uDebug('$array1',$array1);
$smarty->uDebug('$var1'$var1);
$smarty->uDebug('MyOwnQRY',$MyOwnQRY);
// If YourOwnQRY is very long You'll undestand why I added this action based on '$' at the first position of the first parameter Smile

...
?>[/php:1:66a65f65f4]

Is that all ?

- No for sure not You've now a few hours chasing the old 'echo' & 'print_r'stuff in your scripts
Laughing

Hope that's Helpful
Regards
Back to top
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Fri Feb 10, 2006 8:12 pm    Post subject: Reply with quote

This is awesome!! Woohoo no longer must I constantly type

[php:1:f87d6e78ef]
echo "<pre>";
print_r($_POST);
echo "</pre>";
[/php:1:f87d6e78ef]

Kudos to you!
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Hielke Hoeve
Smarty Elite


Joined: 06 Jan 2006
Posts: 406
Location: Netherlands

PostPosted: Fri Feb 10, 2006 8:35 pm    Post subject: Reply with quote

You can also assign the $_POST (or any other wanted item) var to the template engine and view it in the debug popup.

Editing a site while "in production mode" is not sensible imho, but you idea is a good one. This way people can't see the debug info they don't need.
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Mon Feb 13, 2006 5:52 pm    Post subject: Reply with quote

I agree that this might be the easier way to just assign it to Smarty so that it is output in the debug panel...

But at the same time, I think the idea of making a seperate method makes sense.. The whole point of the templating engine is to seperate presentation from logic...

If I start assigning a bunch of variables to Smarty for debugging purposes that have no direct relation to what is being displayed in the current templates... Well it would get what I consider messy once the app grows larger.

Doing it this way you have a seperate category of "application" variables in addition to the regular "display" variables.
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP