blob: a814877858e54eb6585e3414de0412a7f0c7438c [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 Allard8039d4c2008-05-31 02:47:56 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Derek Jonesfd93d222008-05-06 15:18:50 +00006<title>Template Parser Class : CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00007
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
admin17a890d2006-09-27 20:42:42 +000011<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000012<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000013<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000014<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000015
adminb0dd10f2006-08-25 17:25:49 +000016<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 Jones1ca3fc42008-06-27 00:19:33 +000031<td><h1>CodeIgniter User Guide Version 1.6.3</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
Derek Allard866b8132008-04-21 22:02:37 +000089<p><strong>Also Note:</strong> The Template Parser Class is <strong>not</strong> a
90full-blown template parsing solution. We've kept it very lean on purpose in order to maintain maximum performance.</p>
adminb0dd10f2006-08-25 17:25:49 +000091
92
93<h2>Initializing the Class</h2>
94
Derek Allardd2df9bc2007-04-15 17:41:17 +000095<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 +000096
97<code>$this->load->library('parser');</code>
98<p>Once loaded, the Parser library object will be available using: <dfn>$this->parser</dfn></p>
adminb1fddc02006-10-09 21:29:07 +000099
adminb0dd10f2006-08-25 17:25:49 +0000100
101<p>The following functions are available in this library:</p>
102
103<h2>$this->parser->parse()</h2>
104
Derek Allard866b8132008-04-21 22:02:37 +0000105<p>This method accepts a template name and data array as input, and it generates a parsed version. Example:</p>
adminb0dd10f2006-08-25 17:25:49 +0000106
107<code>$this->load->library('parser');<br />
108<br />
109$data = array(<br />
110&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title' => 'My Blog Title',<br />
111&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading'<br />
112&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
113<br />
114$this->parser->parse('blog_template', $data);</code>
115
116<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),
117and the second parameter contains an associative array of data to be replaced in the template. In the above example, the
118template would contain two variables: {blog_title} and {blog_heading}</p>
119
120<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 +0000121passed 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 +0000122pass TRUE (boolean) to the third parameter:</p>
123
124<code>$string = $this->parser->parse('blog_template', $data, TRUE);</code>
125
126
127<h2>Variable Pairs</h2>
128
129<p>The above example code allows simple variables to be replaced. What if you would like an entire block of variables to be
130repeated, with each iteration containing new values? Consider the template example we showed at the top of the page:</p>
131
132<code>&lt;html&gt;<br />
133&lt;head&gt;<br />
134&lt;title&gt;<kbd>{blog_title}</kbd>&lt;/title&gt;<br />
135&lt;/head&gt;<br />
136&lt;body&gt;<br />
137<br />
138&lt;h3&gt;<kbd>{blog_heading}</kbd>&lt;/h3&gt;<br />
139<br />
140<kbd>{blog_entries}</kbd><br />
141&lt;h5&gt;<kbd>{title}</kbd>&lt;/h5&gt;<br />
142&lt;p&gt;<kbd>{body}</kbd>&lt;/p&gt;<br />
143<kbd>{/blog_entries}</kbd><br />
144
145&lt;/body&gt;<br />
146&lt;/html&gt;</code>
147
148<p>In the above code you'll notice a pair of variables: <kbd>{blog_entries}</kbd> data... <kbd>{/blog_entries}</kbd>.
149In a case like this, the entire chunk of data between these pairs would be repeated multiple times, corresponding
150to the number of rows in a result.</p>
151
admine334c472006-10-21 19:44:22 +0000152<p>Parsing variable pairs is done using the identical code shown above to parse single variables,
adminb0dd10f2006-08-25 17:25:49 +0000153except, you will add a multi-dimensional array corresponding to your variable pair data.
154Consider this example:</p>
155
156
157<code>$this->load->library('parser');<br />
158<br />
159$data = array(<br />
160&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title'&nbsp;&nbsp; => 'My Blog Title',<br />
161&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading',<br />
162&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_entries' => array(<br />
163&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 />
164&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 />
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 3', 'body' => 'Body 3'),<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 4', 'body' => 'Body 4'),<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 5', 'body' => 'Body 5')<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;)<br />
169&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
170<br />
171$this->parser->parse('blog_template', $data);</code>
172
173<p>If your "pair" data is coming from a database result, which is already a multi-dimensional array, you can simply
Derek Allard866b8132008-04-21 22:02:37 +0000174use the database result_array() function:</p>
adminb0dd10f2006-08-25 17:25:49 +0000175
176<code>
177$query = $this->db->query("SELECT * FROM blog");<br />
178<br />
179$this->load->library('parser');<br />
180<br />
181$data = array(<br />
182&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title'&nbsp;&nbsp; => 'My Blog Title',<br />
183&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading',<br />
184&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_entries' => $query->result_array()<br />
185&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
186<br />
187$this->parser->parse('blog_template', $data);</code>
188
189
190
191
192</div>
193<!-- END CONTENT -->
194
195
196<div id="footer">
197<p>
198Previous Topic:&nbsp;&nbsp;<a href="trackback.html">Trackback Class</a>
199&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
200<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
201<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Derek Allard9da4dbc2007-04-03 11:39:35 +0000202Next Topic:&nbsp;&nbsp;<a href="unit_testing.html">Unit Testing Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000203</p>
Derek Jones07870432008-02-13 03:49:26 +0000204<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000205</div>
206
207</body>
208</html>