<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Garrick Cheung &#187; Smarty</title>
	<atom:link href="http://www.garrickcheung.com/tag/smarty/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.garrickcheung.com</link>
	<description>Sharing what I know and learn about CSS, MooTools, Javascript, PHP and etc.</description>
	<lastBuildDate>Wed, 28 Apr 2010 02:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Smarty Debug with Firebug</title>
		<link>http://www.garrickcheung.com/javascript/smarty-debug-with-firebug/</link>
		<comments>http://www.garrickcheung.com/javascript/smarty-debug-with-firebug/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 23:44:44 +0000</pubDate>
		<dc:creator>Garrick</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://www.garrickcheung.com/?p=314</guid>
		<description><![CDATA[I use Smarty, a template engine, at work. One of the most awesome things about it is the debug window, where it can list all the templates being used and variables that are available. Mike Horn shared with me how a user, named Hipska, created a Smarty debug template file that works with Firebug. I [...]]]></description>
			<content:encoded><![CDATA[<p>I use <a href="http://www.smarty.net" target="_blank">Smarty</a>, a template engine, at work. One of the most awesome things about it is the debug window, where it can list all the templates being used and variables that are available.</p>
<p><a href="http://www.thegpbkids.com/" target="_blank">Mike Horn</a> shared with me how a <a href="http://www.phphulp.nl/php/print/5/1540/0/scripts/" target="_blank">user, named Hipska, created a Smarty debug template</a> file that works with Firebug. I thought it was so awesome and decided to share it with you too, but with a slight tweak of my own.<span id="more-314"></span></p>
<h3>The Code</h3>
<p>Here&#8217;s the code. I&#8217;ll explain how it works right after.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
</pre></td><td class="code"><pre class="smarty" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">{* Smarty *}</span>
<span style="color: #008080; font-style: italic;">{* debug.tpl, firebug version by Hipska, tweaked by GarrickCheung *}</span>
<span style="color: #D36900;">&#123;</span>assign_debug_info<span style="color: #D36900;">&#125;</span>
<span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">if</span> <span style="color: #0600FF;">isset</span><span style="color: #D36900;">&#40;</span>$_smarty_debug_output<span style="color: #D36900;">&#41;</span> and $_smarty_debug_output <span style="color: #D36900;">eq</span> <span style="color: #ff0000;">&quot;html&quot;</span><span style="color: #D36900;">&#125;</span>
<span style="color: #009000;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot; &quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;&gt;</span>
<span style="color: #009000;">&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot;&gt;</span>
<span style="color: #009000;">&lt;head&gt;</span>
    <span style="color: #009000;">&lt;title&gt;</span>Smarty Debug Console<span style="color: #009000;">&lt;/title&gt;</span>
<span style="color: #D36900;">&#123;</span>literal<span style="color: #D36900;">&#125;</span>
<span style="color: #009000;">&lt;style type=&quot;text/css&quot;&gt;</span>
/* <span style="color: #009000;">&lt;![CDATA[ */</span>
<span style="color: #009000;">body, h1, h2, td, th, p {</span>
<span style="color: #009000;">    font-family: sans-serif;</span>
<span style="color: #009000;">    font-weight: normal;</span>
<span style="color: #009000;">    font-size: 0.9em;</span>
<span style="color: #009000;">    margin: 1px;</span>
<span style="color: #009000;">    padding: 0;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">h1 {</span>
<span style="color: #009000;">    margin: 0;</span>
<span style="color: #009000;">    text-align: left;</span>
<span style="color: #009000;">    padding: 2px;</span>
<span style="color: #009000;">    background-color: #f0c040;</span>
<span style="color: #009000;">    color:  black;</span>
<span style="color: #009000;">    font-weight: bold;</span>
<span style="color: #009000;">    font-size: 1.2em;</span>
<span style="color: #009000;"> }</span>
&nbsp;
<span style="color: #009000;">h2 {</span>
<span style="color: #009000;">    background-color: #9B410E;</span>
<span style="color: #009000;">    color: white;</span>
<span style="color: #009000;">    text-align: left;</span>
<span style="color: #009000;">    font-weight: bold;</span>
<span style="color: #009000;">    padding: 2px;</span>
<span style="color: #009000;">    border-top: 1px solid black;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">body {</span>
<span style="color: #009000;">    background: black; </span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">p, table, div {</span>
<span style="color: #009000;">    background: #f0ead8;</span>
<span style="color: #009000;">} </span>
&nbsp;
<span style="color: #009000;">p {</span>
<span style="color: #009000;">    margin: 0;</span>
<span style="color: #009000;">    font-style: italic;</span>
<span style="color: #009000;">    text-align: center;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">table {</span>
<span style="color: #009000;">    width: 100%;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">th, td {</span>
<span style="color: #009000;">    font-family: monospace;</span>
<span style="color: #009000;">    vertical-align: top;</span>
<span style="color: #009000;">    text-align: left;</span>
<span style="color: #009000;">    width: 50%;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">td {</span>
<span style="color: #009000;">    color: green;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">.odd {</span>
<span style="color: #009000;">    background-color: #eeeeee;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">.even {</span>
<span style="color: #009000;">    background-color: #fafafa;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">.exectime {</span>
<span style="color: #009000;">    font-size: 0.8em;</span>
<span style="color: #009000;">    font-style: italic;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">#table_assigned_vars th {</span>
<span style="color: #009000;">    color: blue;</span>
<span style="color: #009000;">}</span>
&nbsp;
<span style="color: #009000;">#table_config_vars th {</span>
<span style="color: #009000;">    color: maroon;</span>
<span style="color: #009000;">}</span>
<span style="color: #009000;">/* ]]&gt;</span> */
<span style="color: #009000;">&lt;/style&gt;</span>
<span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span>literal<span style="color: #D36900;">&#125;</span>
<span style="color: #009000;">&lt;/head&gt;</span>
<span style="color: #009000;">&lt;body&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;h1&gt;</span>Smarty Debug Console<span style="color: #009000;">&lt;/h1&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;h2&gt;</span>included templates &amp;amp; config files (load time in seconds)<span style="color: #009000;">&lt;/h2&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;div&gt;</span>
<span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">section</span> <span style="color: #6A0A0A;">name</span><span style="color: #D36900;">=</span>templates <span style="color: #6A0A0A;">loop</span><span style="color: #D36900;">=</span>$_debug_tpls<span style="color: #D36900;">&#125;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">section</span> <span style="color: #6A0A0A;">name</span><span style="color: #D36900;">=</span><span style="color: #008000;">indent</span> <span style="color: #6A0A0A;">loop</span><span style="color: #D36900;">=</span>$_debug_tpls<span style="color: #D36900;">&#91;</span>templates<span style="color: #D36900;">&#93;</span>.<span style="color: #006600;">depth</span><span style="color: #D36900;">&#125;</span>&amp;nbsp;&amp;nbsp;&amp;nbsp;<span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span><span style="color: #0600FF;">section</span><span style="color: #D36900;">&#125;</span>
    <span style="color: #009000;">&lt;font color={if $_debug_tpls[templates].type eq &quot;template&quot;}brown{elseif $_debug_tpls[templates].type eq &quot;insert&quot;}black{else}green{/if}&gt;</span>
        <span style="color: #D36900;">&#123;</span>$_debug_tpls<span style="color: #D36900;">&#91;</span>templates<span style="color: #D36900;">&#93;</span>.<span style="color: #006600;">filename</span>|escape:html<span style="color: #D36900;">&#125;</span><span style="color: #009000;">&lt;/font&gt;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">if</span> <span style="color: #0600FF;">isset</span><span style="color: #D36900;">&#40;</span>$_debug_tpls<span style="color: #D36900;">&#91;</span>templates<span style="color: #D36900;">&#93;</span>.<span style="color: #006600;">exec_time</span><span style="color: #D36900;">&#41;</span><span style="color: #D36900;">&#125;</span>
        <span style="color: #009000;">&lt;span class=&quot;exectime&quot;&gt;</span>
        (<span style="color: #D36900;">&#123;</span>$_debug_tpls<span style="color: #D36900;">&#91;</span>templates<span style="color: #D36900;">&#93;</span>.<span style="color: #006600;">exec_time</span>|string_format:<span style="color: #ff0000;">&quot;%.5f&quot;</span><span style="color: #D36900;">&#125;</span>)
        <span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">if</span> <span style="color: #D36900;">%</span>templates.<span style="color: #006600;">index</span><span style="color: #D36900;">%</span> <span style="color: #D36900;">eq</span> <span style="color: #cc66cc;">0</span><span style="color: #D36900;">&#125;</span>(total)<span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span><span style="color: #0600FF;">if</span><span style="color: #D36900;">&#125;</span>
        <span style="color: #009000;">&lt;/span&gt;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span><span style="color: #0600FF;">if</span><span style="color: #D36900;">&#125;</span>
    <span style="color: #009000;">&lt;br /&gt;</span>
<span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">sectionelse</span><span style="color: #D36900;">&#125;</span>
    <span style="color: #009000;">&lt;p&gt;</span>no templates included<span style="color: #009000;">&lt;/p&gt;</span>
<span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span><span style="color: #0600FF;">section</span><span style="color: #D36900;">&#125;</span>
<span style="color: #009000;">&lt;/div&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;h2&gt;</span>assigned template variables<span style="color: #009000;">&lt;/h2&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;table id=&quot;table_assigned_vars&quot;&gt;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">section</span> <span style="color: #6A0A0A;">name</span><span style="color: #D36900;">=</span>vars <span style="color: #6A0A0A;">loop</span><span style="color: #D36900;">=</span>$_debug_keys<span style="color: #D36900;">&#125;</span>
        <span style="color: #009000;">&lt;tr class=&quot;{cycle values=&quot;odd,even&quot;}&quot;&gt;</span>
            <span style="color: #009000;">&lt;th&gt;</span><span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">ldelim</span><span style="color: #D36900;">&#125;</span>$<span style="color: #D36900;">&#123;</span>$_debug_keys<span style="color: #D36900;">&#91;</span>vars<span style="color: #D36900;">&#93;</span>|escape:<span style="color: #ff0000;">'html'</span><span style="color: #D36900;">&#125;</span><span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">rdelim</span><span style="color: #D36900;">&#125;</span><span style="color: #009000;">&lt;/th&gt;</span>
            <span style="color: #009000;">&lt;td&gt;</span><span style="color: #D36900;">&#123;</span>$_debug_vals<span style="color: #D36900;">&#91;</span>vars<span style="color: #D36900;">&#93;</span>|@debug_print_var<span style="color: #D36900;">&#125;</span><span style="color: #009000;">&lt;/td&gt;&lt;/tr&gt;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">sectionelse</span><span style="color: #D36900;">&#125;</span>
        <span style="color: #009000;">&lt;tr&gt;&lt;td&gt;&lt;p&gt;</span>no template variables assigned<span style="color: #009000;">&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span><span style="color: #0600FF;">section</span><span style="color: #D36900;">&#125;</span>
<span style="color: #009000;">&lt;/table&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;h2&gt;</span>assigned config file variables (outer template scope)<span style="color: #009000;">&lt;/h2&gt;</span>
&nbsp;
<span style="color: #009000;">&lt;table id=&quot;table_config_vars&quot;&gt;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">section</span> <span style="color: #6A0A0A;">name</span><span style="color: #D36900;">=</span>config_vars <span style="color: #6A0A0A;">loop</span><span style="color: #D36900;">=</span>$_debug_config_keys<span style="color: #D36900;">&#125;</span>
        <span style="color: #009000;">&lt;tr class=&quot;{cycle values=&quot;odd,even&quot;}&quot;&gt;</span>
            <span style="color: #009000;">&lt;th&gt;</span><span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">ldelim</span><span style="color: #D36900;">&#125;</span>#<span style="color: #D36900;">&#123;</span>$_debug_config_keys<span style="color: #D36900;">&#91;</span>config_vars<span style="color: #D36900;">&#93;</span>|escape:<span style="color: #ff0000;">'html'</span><span style="color: #D36900;">&#125;</span>#<span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">rdelim</span><span style="color: #D36900;">&#125;</span><span style="color: #009000;">&lt;/th&gt;</span>
            <span style="color: #009000;">&lt;td&gt;</span><span style="color: #D36900;">&#123;</span>$_debug_config_vals<span style="color: #D36900;">&#91;</span>config_vars<span style="color: #D36900;">&#93;</span>|@debug_print_var<span style="color: #D36900;">&#125;</span><span style="color: #009000;">&lt;/td&gt;&lt;/tr&gt;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">sectionelse</span><span style="color: #D36900;">&#125;</span>
        <span style="color: #009000;">&lt;tr&gt;&lt;td&gt;&lt;p&gt;</span>no config vars assigned<span style="color: #009000;">&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;</span>
    <span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span><span style="color: #0600FF;">section</span><span style="color: #D36900;">&#125;</span>
<span style="color: #009000;">&lt;/table&gt;</span>
<span style="color: #009000;">&lt;/body&gt;</span>
<span style="color: #009000;">&lt;/html&gt;</span>
<span style="color: #D36900;">&#123;</span><span style="color: #0600FF;">else</span><span style="color: #D36900;">&#125;</span>
<span style="color: #009000;">&lt;script type=&quot;text/javascript&quot;&gt;</span>
// <span style="color: #009000;">&lt;![CDATA[</span>
<span style="color: #009000;">var Smarty_debug = function Smarty_debug(collapsed){ldelim}</span>
&nbsp;
<span style="color: #009000;">	var group = (collapsed) ? console.groupCollapsed : console.group;</span>
&nbsp;
<span style="color: #009000;">	group(&quot;Smarty Debug&quot;);</span>
&nbsp;
<span style="color: #009000;">	group(&quot;Included templates &amp; config files&quot;);</span>
<span style="color: #009000;">	{section name=templates loop=$_debug_tpls}</span>
<span style="color: #009000;">		 console.log(&quot;{$_debug_tpls[templates].filename|escape:javascript}{if isset($_debug_tpls[templates].exec_time)} - {$_debug_tpls[templates].exec_time|string_format:&quot;%.5f&quot;}{if %templates.index% eq 0} (total){/if}{/if}&quot;);</span>
<span style="color: #009000;">	{sectionelse}</span>
<span style="color: #009000;">		 console.info(&quot;no templates included&quot;);</span>
<span style="color: #009000;">	{/section}</span>
<span style="color: #009000;">	console.groupEnd();</span>
&nbsp;
<span style="color: #009000;">	group(&quot;Assigned template variables&quot;);</span>
<span style="color: #009000;">	{section name=vars loop=$_debug_keys}</span>
<span style="color: #009000;">		 console.log(&quot;{ldelim}${$_debug_keys[vars]|escape:'javascript'}{rdelim}:&quot;, {$_debug_vals[vars]|@json_encode});</span>
<span style="color: #009000;">	{sectionelse}</span>
<span style="color: #009000;">		 console.info(&quot;no template variables assigned&quot;);</span>
<span style="color: #009000;">	{/section}</span>
<span style="color: #009000;">	console.groupEnd();</span>
&nbsp;
<span style="color: #009000;">	group(&quot;Assigned config file variables&quot;);</span>
<span style="color: #009000;">	{section name=config_vars loop=$_debug_config_keys}</span>
<span style="color: #009000;">		 console.log(&quot;{ldelim}#{$_debug_config_keys[config_vars]|escape:'javascript'}#{rdelim}:&quot;, {$_debug_config_vals[config_vars]|@json_encode});</span>
<span style="color: #009000;">	{sectionelse}</span>
<span style="color: #009000;">		 console.info(&quot;no config file variables assigned&quot;);</span>
<span style="color: #009000;">	{/section}</span>
<span style="color: #009000;">	console.groupEnd();</span>
&nbsp;
<span style="color: #009000;">	console.groupEnd();</span>
<span style="color: #009000;">	return &quot;Smarty version {$smarty.version}&quot;;</span>
&nbsp;
<span style="color: #009000;">{rdelim};</span>
<span style="color: #009000;">// ]]&gt;</span>
<span style="color: #009000;">&lt;/script&gt;</span>
<span style="color: #D36900;">&#123;</span><span style="color: #D36900;">/</span><span style="color: #0600FF;">if</span><span style="color: #D36900;">&#125;</span></pre></td></tr></table></div>

<h3>How it Works</h3>
<p>This debug template gives you two options on how to view it: the original method display&#8217;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&#8217;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.</p>
<p>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&#8217;t support console, then you probably shouldn&#8217;t be using this method, that&#8217;s why the HTML version is available.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Smarty_debug<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//type this in your Firebug Console</span></pre></td></tr></table></div>

<p>It bothered me a little that Hipska&#8217;s method printed out every line in the group once Smarty_debug function is called. Now here&#8217;s my tweak: I&#8217;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.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Smarty_debug<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//prints out a collapsed group</span></pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>I really love being able to print out the Smarty debug data to the Firebug Console because I won&#8217;t have to deal with an additional pop-up every time. I get&#8217;s a little annoying and I think it suck&#8217;s up more memory. Thank you Hipska and Mike!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.garrickcheung.com/javascript/smarty-debug-with-firebug/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
