blob: 4bf63efb8b6b898aa045f319ac65638da3c26741 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<title>Template Parser Class : CodeIgniter User Guide</title>
7
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
11<script type="text/javascript" src="../nav/nav.js"></script>
12<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13<script type="text/javascript" src="../nav/moo.fx.js"></script>
14<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='ExpressionEngine Dev Team' />
20<meta name='description' content='CodeIgniter User Guide' />
21
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_darker.jpg" width="154" height="43" 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 Jonesbbedc762009-09-11 14:47:37 +000031<td><h1>CodeIgniter User Guide Version 1.7.2</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33</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">
43<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Template Parser Class
46</td>
47<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>
48</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
85<p class="important"><strong>Note:</strong> CodeIgniter does <strong>not</strong> require you to use this class
86since using pure PHP in your view pages lets them run a little faster. However, some developers prefer to use a template engine if
87they 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> a
90full-blown template parsing solution. We've kept it very lean on purpose in order to maintain maximum performance.</p>
91
92
93<h2>Initializing the Class</h2>
94
95<p>Like most other classes in CodeIgniter, the Parser class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
96
97<code>$this->load->library('parser');</code>
98<p>Once loaded, the Parser library object will be available using: <dfn>$this->parser</dfn></p>
99
100
101<p>The following functions are available in this library:</p>
102
103<h2>$this->parser->parse()</h2>
104
105<p>This method accepts a template name and data array as input, and it generates a parsed version. Example:</p>
106
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
121passed 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
122pass 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
152<p>Parsing variable pairs is done using the identical code shown above to parse single variables,
153except, 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
174use the database result_array() function:</p>
175
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;
202Next Topic:&nbsp;&nbsp;<a href="typography.html">Typography</a>
203</p>
Derek Jones7f3719f2010-01-05 13:35:37 +0000204<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2010 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000205</div>
206
207</body>
adminb0dd10f2006-08-25 17:25:49 +0000208</html>