blob: 51f529739ea45b0f494b96d1c07af01d5bf47545 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminb0dd10f2006-08-25 17:25:49 +00003<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Template Parser Class</title>
adminb0dd10f2006-08-25 17:25:49 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Allard197d10b2008-02-12 04:20:38 +000031<td><h1>CodeIgniter User Guide Version 1.6.1</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Jones7a9193a2008-01-21 18:39:20 +000043<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Template Parser Class
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminb0dd10f2006-08-25 17:25:49 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59
60
61<h1>Template Parser Class</h1>
62
63<p>The Template Parser Class enables you to parse pseudo-variables contained within your view files. It can parse simple
64variables or variable tag pairs. If you've never used a template engine, pseudo-variables look like this:</p>
65
66<code>&lt;html&gt;<br />
67&lt;head&gt;<br />
68&lt;title&gt;<kbd>{blog_title}</kbd>&lt;/title&gt;<br />
69&lt;/head&gt;<br />
70&lt;body&gt;<br />
71<br />
72&lt;h3&gt;<kbd>{blog_heading}</kbd>&lt;/h3&gt;<br />
73<br />
74<kbd>{blog_entries}</kbd><br />
75&lt;h5&gt;<kbd>{title}</kbd>&lt;/h5&gt;<br />
76&lt;p&gt;<kbd>{body}</kbd>&lt;/p&gt;<br />
77<kbd>{/blog_entries}</kbd><br />
78
79&lt;/body&gt;<br />
80&lt;/html&gt;</code>
81
82<p>These variables are not actual PHP variables, but rather plain text representations that allow you to eliminate
83PHP from your templates (view files).</p>
84
Derek Allardd2df9bc2007-04-15 17:41:17 +000085<p class="important"><strong>Note:</strong> CodeIgniter does <strong>not</strong> require you to use this class
admine334c472006-10-21 19:44:22 +000086since using pure PHP in your view pages lets them run a little faster. However, some developers prefer to use a template engine if
adminb0dd10f2006-08-25 17:25:49 +000087they work with designers who they feel would find some confusion working with PHP.</p>
88
89<p><strong>Also Note:</strong> The Template Parser Class is <strong>not</strong> not a
90full-blown template parsing solution. We've kept it very lean on purpose in order to maintain maximum performance.
91
92</p>
93
94
95<h2>Initializing the Class</h2>
96
Derek Allardd2df9bc2007-04-15 17:41:17 +000097<p>Like most other classes in CodeIgniter, the Parser class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
adminb0dd10f2006-08-25 17:25:49 +000098
99<code>$this->load->library('parser');</code>
100<p>Once loaded, the Parser library object will be available using: <dfn>$this->parser</dfn></p>
adminb1fddc02006-10-09 21:29:07 +0000101
adminb0dd10f2006-08-25 17:25:49 +0000102
103<p>The following functions are available in this library:</p>
104
105<h2>$this->parser->parse()</h2>
106
107<p>This variable accepts a template name and data array as input, and it generates a parsed version. Example:</p>
108
109<code>$this->load->library('parser');<br />
110<br />
111$data = array(<br />
112&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title' => 'My Blog Title',<br />
113&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading'<br />
114&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
115<br />
116$this->parser->parse('blog_template', $data);</code>
117
118<p>The first parameter contains the name of the <a href="../general/views.html">view file</a> (in this example the file would be called blog_template.php),
119and the second parameter contains an associative array of data to be replaced in the template. In the above example, the
120template would contain two variables: {blog_title} and {blog_heading}</p>
121
122<p>There is no need to "echo" or do something with the data returned by <dfn>$this->parser->parse()</dfn>. It is automatically
admine334c472006-10-21 19:44:22 +0000123passed to the output class to be sent to the browser. However, if you do want the data returned instead of sent to the output class you can
adminb0dd10f2006-08-25 17:25:49 +0000124pass TRUE (boolean) to the third parameter:</p>
125
126<code>$string = $this->parser->parse('blog_template', $data, TRUE);</code>
127
128
129<h2>Variable Pairs</h2>
130
131<p>The above example code allows simple variables to be replaced. What if you would like an entire block of variables to be
132repeated, with each iteration containing new values? Consider the template example we showed at the top of the page:</p>
133
134<code>&lt;html&gt;<br />
135&lt;head&gt;<br />
136&lt;title&gt;<kbd>{blog_title}</kbd>&lt;/title&gt;<br />
137&lt;/head&gt;<br />
138&lt;body&gt;<br />
139<br />
140&lt;h3&gt;<kbd>{blog_heading}</kbd>&lt;/h3&gt;<br />
141<br />
142<kbd>{blog_entries}</kbd><br />
143&lt;h5&gt;<kbd>{title}</kbd>&lt;/h5&gt;<br />
144&lt;p&gt;<kbd>{body}</kbd>&lt;/p&gt;<br />
145<kbd>{/blog_entries}</kbd><br />
146
147&lt;/body&gt;<br />
148&lt;/html&gt;</code>
149
150<p>In the above code you'll notice a pair of variables: <kbd>{blog_entries}</kbd> data... <kbd>{/blog_entries}</kbd>.
151In a case like this, the entire chunk of data between these pairs would be repeated multiple times, corresponding
152to the number of rows in a result.</p>
153
admine334c472006-10-21 19:44:22 +0000154<p>Parsing variable pairs is done using the identical code shown above to parse single variables,
adminb0dd10f2006-08-25 17:25:49 +0000155except, you will add a multi-dimensional array corresponding to your variable pair data.
156Consider this example:</p>
157
158
159<code>$this->load->library('parser');<br />
160<br />
161$data = array(<br />
162&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title'&nbsp;&nbsp; => 'My Blog Title',<br />
163&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading',<br />
164&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_entries' => array(<br />
165&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('title' => 'Title 1', 'body' => 'Body 1'),<br />
166&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('title' => 'Title 2', 'body' => 'Body 2'),<br />
167&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('title' => 'Title 3', 'body' => 'Body 3'),<br />
168&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('title' => 'Title 4', 'body' => 'Body 4'),<br />
169&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('title' => 'Title 5', 'body' => 'Body 5')<br />
170&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />
171&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
172<br />
173$this->parser->parse('blog_template', $data);</code>
174
175<p>If your "pair" data is coming from a database result, which is already a multi-dimensional array, you can simply
176use the database result function:</p>
177
178<code>
179$query = $this->db->query("SELECT * FROM blog");<br />
180<br />
181$this->load->library('parser');<br />
182<br />
183$data = array(<br />
184&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title'&nbsp;&nbsp; => 'My Blog Title',<br />
185&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading',<br />
186&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_entries' => $query->result_array()<br />
187&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
188<br />
189$this->parser->parse('blog_template', $data);</code>
190
191
192
193
194</div>
195<!-- END CONTENT -->
196
197
198<div id="footer">
199<p>
200Previous Topic:&nbsp;&nbsp;<a href="trackback.html">Trackback Class</a>
201&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
202<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
203<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Derek Allard9da4dbc2007-04-03 11:39:35 +0000204Next Topic:&nbsp;&nbsp;<a href="unit_testing.html">Unit Testing Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000205</p>
Derek Jones7a9193a2008-01-21 18:39:20 +0000206<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000207</div>
208
209</body>
210</html>