代码改变世界

how to debug smarty with firebug

2012-07-24 13:56  wildboar  阅读(589)  评论(0编辑  收藏  举报

the article comes from the link following, I just want to backup it in my blog. http://www.garrickcheung.com/javascript/smarty-debug-with-firebug/

 

  1 {capture name='_smarty_debug' assign=debug_output}
  2     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3     <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  4         <head>
  5             <title>Smarty Debug Console</title>
  6             <style type="text/css">
  7                 {literal}
  8                     body, h1, h2, td, th, p {
  9                         font-family: sans-serif;
 10                         font-weight: normal;
 11                         font-size: 0.9em;
 12                         margin: 1px;
 13                         padding: 0;
 14                     }
 15 
 16                     h1 {
 17                         margin: 0;
 18                         text-align: left;
 19                         padding: 2px;
 20                         background-color: #f0c040;
 21                         color:  black;
 22                         font-weight: bold;
 23                         font-size: 1.2em;
 24                     }
 25 
 26                     h2 {
 27                         background-color: #9B410E;
 28                         color: white;
 29                         text-align: left;
 30                         font-weight: bold;
 31                         padding: 2px;
 32                         border-top: 1px solid black;
 33                     }
 34 
 35                     body {
 36                         background: black; 
 37                     }
 38 
 39                     p, table, div {
 40                         background: #f0ead8;
 41                     } 
 42 
 43                     p {
 44                         margin: 0;
 45                         font-style: italic;
 46                         text-align: center;
 47                     }
 48 
 49                     table {
 50                         width: 100%;
 51                     }
 52 
 53                     th, td {
 54                         font-family: monospace;
 55                         vertical-align: top;
 56                         text-align: left;
 57                         width: 50%;
 58                     }
 59 
 60                     td {
 61                         color: green;
 62                     }
 63 
 64                     .odd {
 65                         background-color: #eeeeee;
 66                     }
 67 
 68                     .even {
 69                         background-color: #fafafa;
 70                     }
 71 
 72                     .exectime {
 73                         font-size: 0.8em;
 74                         font-style: italic;
 75                     }
 76 
 77                     #table_assigned_vars th {
 78                         color: blue;
 79                     }
 80 
 81                     #table_config_vars th {
 82                         color: maroon;
 83                     }
 84                 {/literal}
 85             </style>
 86         </head>
 87         <body>
 88 
 89             <h1>Smarty Debug Console  -  {if isset($template_name)}{$template_name|debug_print_var nofilter}{else}Total Time {$execution_time|string_format:"%.5f"}{/if}</h1>
 90 
 91             {if !empty($template_data)}
 92                 <h2>included templates &amp; config files (load time in seconds)</h2>
 93 
 94                 <div>
 95                     {foreach $template_data as $template}
 96                         <font color=brown>{$template.name}</font>
 97                         <span class="exectime">
 98                             (compile {$template['compile_time']|string_format:"%.5f"}) (render {$template['render_time']|string_format:"%.5f"}) (cache {$template['cache_time']|string_format:"%.5f"})
 99                         </span>
100                         <br>
101                         {/foreach}
102                 </div>
103             {/if}
104 
105             <h2>assigned template variables</h2>
106 
107             <table id="table_assigned_vars">
108                 {foreach $assigned_vars as $vars}
109                     <tr class="{if $vars@iteration % 2 eq 0}odd{else}even{/if}">   
110                         <th>${$vars@key|escape:'html'}</th>
111                         <td>{$vars|debug_print_var nofilter}</td></tr>
112                     {/foreach}
113             </table>
114 
115             <h2>assigned config file variables (outer template scope)</h2>
116 
117             <table id="table_config_vars">
118                 {foreach $config_vars as $vars}
119                     <tr class="{if $vars@iteration % 2 eq 0}odd{else}even{/if}">   
120                         <th>{$vars@key|escape:'html'}</th>
121                         <td>{$vars|debug_print_var nofilter}</td></tr>
122                     {/foreach}
123 
124             </table>
125         </body>
126     </html>
127 {/capture}
128 <script type="text/javascript">
129     {$id = $template_name|default:''|md5}
130         _smarty_console = window.open("","console{$id}","width=680,height=600,resizable,scrollbars=yes");
131         _smarty_console.document.write("{$debug_output|escape:'javascript' nofilter}");
132         _smarty_console.document.close();
133 </script>

 

 

 

How it Works

This debug template gives you two options on how to view it: the original method display’s everything in a new pop-up window, the new and awesome method shows the same thing but in the Firebug Console. The template has an if statement/flag that checks whether it should be rendered in HTML or use Firebug. You’ll have to do some digging on how to setup this flag in your configuration, but the flag is in line 4 defined as $_smarty_debug_output.

The default is to use Firebug. A function named Smarty_debug is defined at the bottom at line 143. When you run it in the Firebug console, it will print out the Smarty debug information in the console as collapsible groups. If the browser doesn’t support console, then you probably shouldn’t be using this method, that’s why the HTML version is available.

1 Smarty_debug(); //type this in your Firebug Console

It bothered me a little that Hipska’s method printed out every line in the group once Smarty_debug function is called. Now here’s my tweak: I’ve made it optional to print out as a collapsed group but you WILL need Firebug 1.3+ (if it works on other versions, please let me know). All you need to do is supply a boolean argument.

1 Smarty_debug(true); //prints out a collapsed group