| View previous topic :: View next topic |
| Author |
Message |
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Thu Oct 26, 2006 2:01 pm Post subject: debug output somewhat imprecise |
|
|
Testing:
| Code: | <?php
require_once 'Smarty.class.php';
/**
* @global Smarty $smarty
*/
$smarty = new Smarty;
$smarty->template_dir = '/www/kfp-online/templates/';
$smarty->compile_dir = '/www/kfp-online/templates_c/';
$smarty->config_dir = '/www/kfp-online/configs/';
$smarty->cache_dir = '/www/kfp-online/cache/';
$smarty->debug = true;
$smarty->assign('bool_true', true);
$smarty->assign('bool_false', false);
$smarty->assign('string_empty', '');
$smarty->assign('null', null);
$smarty->assign('int_zero', 0);
$smarty->assign('string_zero', "0");
$smarty->display('debug.tpl');
?> |
debug window output:
| Code: | {$SCRIPT_NAME} /office/test.php
{$bool_false}
{$bool_true} 1
{$int_zero} 0
{$null} [i]empty[/i]
{$string_empty} [i]empty[/i]
{$string_zero} 0 |
possible solution:
| Code: | function smarty_modifier_debug_print_var($var, $depth = 0, $length = 40)
{
$_replace = array(
"\n" => '<i>\n</i>',
"\r" => '<i>\r</i>',
"\t" => '<i>\t</i>'
);
switch (gettype($var)) {
case 'array' :
$results = '<b>Array (' . count($var) . ')</b>';
foreach ($var as $curr_key => $curr_val) {
$return = smarty_modifier_debug_print_var($curr_val, $depth + 1, $length);
$results .= '<br>' . str_repeat(' ', $depth*2) . '<b>'
. strtr($curr_key, $_replace) . '</b> => ' . $return;
}
break;
case 'object' :
$object_vars = get_object_vars($var);
$results = '<b>' . get_class($var) . ' Object (' . count($object_vars) . ')</b>';
foreach ($var as $curr_key => $curr_val) {
$return = smarty_modifier_debug_print_var($curr_val, $depth + 1, $length);
$results .= '<br>' . str_repeat(' ', $depth*2) . '<b>'
. strtr($curr_key, $_replace) . '</b> => ' . $return;
}
break;
case 'boolean' :
case 'NULL' :
case 'resource' :
if (true === $var) {
$results = 'true';
} elseif (false === $var) {
$results = 'false';
} elseif (null === $var) {
$results = 'null';
} else {
$results = htmlspecialchars((string) $var);
}
$results = '<i>' . $results . '</i>';
break;
case 'integer' :
case 'float' :
$results = htmlspecialchars((string) $var);
break;
case 'string' :
$results = strtr($var, $_replace);
if (strlen($var) > $length ) {
$results = substr($var, 0, $length - 3) . '...';
}
$results = htmlspecialchars('"' . $results . '"');
break;
case 'unknown type' :
default :
$results = strtr((string) $var, $_replace);
if (strlen($results) > $length ) {
$results = substr($results, 0, $length - 3) . '...';
}
$results = htmlspecialchars($results);
}
return $results;
} |
better output:
| Code: | {$SCRIPT_NAME} "/office/test.php"
{$bool_false} [i]false[/i]
{$bool_true} [i]true[/i]
{$int_zero} 0
{$null} [i]null[/i]
{$string_empty} ""
{$string_zero} "0" |
|
|
| Back to top |
|
 |
Hielke Hoeve Smarty Elite
Joined: 06 Jan 2006 Posts: 406 Location: Netherlands
|
Posted: Fri Oct 27, 2006 8:02 am Post subject: |
|
|
Tbh it's no where near a bug, it is just a matter of personal taste.
You say that when a boolean is set to false it shows nothing, which is correct. a boolean is just an enum of 1 or 0. If a variable is null it can also in fact be 0 or "".
I do agree on the string_zero, it could be confusing. But then again others disagree. _________________ Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net> |
|
| Back to top |
|
 |
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Fri Oct 27, 2006 8:21 am Post subject: |
|
|
| Hielke Hoeve wrote: | | Tbh it's no where near a bug, it is just a matter of personal taste. |
of course, but it could help alot if you see more clear what your variables are.
| Hielke Hoeve wrote: | | You say that when a boolean is set to false it shows nothing, which is correct. |
what is correct, what i said or the behavior?
the behavior is of course like above a matter of taste
| Hielke Hoeve wrote: | | a boolean is just an enum of 1 or 0. If a variable is null it can also in fact be 0 or "". |
if a variable is null it can not be 0 or "" or "0" or false, in a loose comparison with '==' this are all equal, but this says not that null can be empty, it is not the same empty is empty and null is undefined
gettype() or '===' will never tell you that en empty variable is null
displaying null instead of empty can help to see that i have assigned an undefined variable by accident
the same goes to all other (1 instead of true, '' instead of false, 0 instead of '0'), it can help me alot to track down wrong assigned variables or return values.
at least following your argument false should also be displayed as empty and not just nothing |
|
| Back to top |
|
 |
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Fri Oct 27, 2006 8:24 am Post subject: |
|
|
| And if i use a debug output it is not interesting 'HOW' a variable CAN BE interpreted, it is more interesting WHAT a variable IS. |
|
| Back to top |
|
 |
Hielke Hoeve Smarty Elite
Joined: 06 Jan 2006 Posts: 406 Location: Netherlands
|
Posted: Fri Oct 27, 2006 8:57 am Post subject: |
|
|
| cybot wrote: | | at least following your argument false should also be displayed as empty and not just nothing |
I agree, showing nothing is confusing. Samen as the string_zero, it should be quoted.
| cybot wrote: | | if a variable is null it can not be 0 or "" or "0" or false, in a loose comparison with '==' this are all equal. |
Exactly. Since it is a loose comparison it can be either of the 3.
If you agree on that it is not a bug, then why post in the Bugs forum  _________________ Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net> |
|
| Back to top |
|
 |
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Fri Oct 27, 2006 9:22 am Post subject: |
|
|
i do not agree that this is not a bug, the debug output presents me wrong or at least ambiguous information about variables
second not all cases (of variable types) are handled correctly and could lead to Notices or even warnings |
|
| Back to top |
|
 |
boots Administrator
Joined: 16 Apr 2003 Posts: 5610 Location: Toronto, Canada
|
Posted: Fri Oct 27, 2006 3:40 pm Post subject: |
|
|
IMHO, cybot's representation in more reasonable.
@Cybot: please consider submiting your proposed changes as a patch against CVS for testing. |
|
| Back to top |
|
 |
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
|
| Back to top |
|
 |
cybot Smarty Regular
Joined: 20 Apr 2005 Posts: 83
|
Posted: Thu Nov 02, 2006 7:14 am Post subject: |
|
|
btw. is_object() returns false on an incomplete object (object without class definition) but produces a FATAL error when casted to string - this can happen with the current code, and is fixed with my patch - gettype() returns 'object' also for incomplete objects
http://php.net/is_object
"Note: This function will return FALSE if used on an unserialized object where the class definition is not present (even though gettype() returns object)."
@Hielke Hoeve: now it is definitely a bug!  |
|
| Back to top |
|
 |
boots Administrator
Joined: 16 Apr 2003 Posts: 5610 Location: Toronto, Canada
|
Posted: Tue Nov 07, 2006 8:33 pm Post subject: |
|
|
Committed in CVS.
Thanks cybot! |
|
| Back to top |
|
 |
Hielke Hoeve Smarty Elite
Joined: 06 Jan 2006 Posts: 406 Location: Netherlands
|
Posted: Wed Nov 08, 2006 8:41 am Post subject: |
|
|
I never disagreed that the output was odd, just said that I didn't see it as a bug. Eitherway happy to see better output  _________________ Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net> |
|
| Back to top |
|
 |
boots Administrator
Joined: 16 Apr 2003 Posts: 5610 Location: Toronto, Canada
|
Posted: Mon Nov 20, 2006 9:08 pm Post subject: |
|
|
| I just committed an additional patch to fix the depth formatting for arrays and objects. Also, I changed the notation for objects slightly. |
|
| Back to top |
|
 |
|