blob: 2f93baf0b1adafb013fd89c9d9ba0ae1ed457329 [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">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
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>
adminb0dd10f2006-08-25 17:25:49 +000013<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<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>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
admin10c3f412006-10-08 07:21:12 +000036<td><h1>Code Igniter User Guide Version 1.5.0b1</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Template Parser Class
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64
65
66<h1>Template Parser Class</h1>
67
68<p>The Template Parser Class enables you to parse pseudo-variables contained within your view files. It can parse simple
69variables or variable tag pairs. If you've never used a template engine, pseudo-variables look like this:</p>
70
71<code>&lt;html&gt;<br />
72&lt;head&gt;<br />
73&lt;title&gt;<kbd>{blog_title}</kbd>&lt;/title&gt;<br />
74&lt;/head&gt;<br />
75&lt;body&gt;<br />
76<br />
77&lt;h3&gt;<kbd>{blog_heading}</kbd>&lt;/h3&gt;<br />
78<br />
79<kbd>{blog_entries}</kbd><br />
80&lt;h5&gt;<kbd>{title}</kbd>&lt;/h5&gt;<br />
81&lt;p&gt;<kbd>{body}</kbd>&lt;/p&gt;<br />
82<kbd>{/blog_entries}</kbd><br />
83
84&lt;/body&gt;<br />
85&lt;/html&gt;</code>
86
87<p>These variables are not actual PHP variables, but rather plain text representations that allow you to eliminate
88PHP from your templates (view files).</p>
89
90<p class="important"><strong>Note:</strong> Code Igniter does <strong>not</strong> require you to use this class
91since using pure PHP in your view pages lets them run a little faster. However, some developers prefer to use a template engine if
92they work with designers who they feel would find some confusion working with PHP.</p>
93
94<p><strong>Also Note:</strong> The Template Parser Class is <strong>not</strong> not a
95full-blown template parsing solution. We've kept it very lean on purpose in order to maintain maximum performance.
96
97</p>
98
99
100<h2>Initializing the Class</h2>
101
102<p>Like most other classes in Code Igniter, the Parser class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
103
104<code>$this->load->library('parser');</code>
105<p>Once loaded, the Parser library object will be available using: <dfn>$this->parser</dfn></p>
adminb1fddc02006-10-09 21:29:07 +0000106<p>You can also set your own class variable name. Please see the <a href="loader.html">Loader Class</a> for more info.</p>
107
adminb0dd10f2006-08-25 17:25:49 +0000108
109<p>The following functions are available in this library:</p>
110
111<h2>$this->parser->parse()</h2>
112
113<p>This variable accepts a template name and data array as input, and it generates a parsed version. Example:</p>
114
115<code>$this->load->library('parser');<br />
116<br />
117$data = array(<br />
118&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title' => 'My Blog Title',<br />
119&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading'<br />
120&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
121<br />
122$this->parser->parse('blog_template', $data);</code>
123
124<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),
125and the second parameter contains an associative array of data to be replaced in the template. In the above example, the
126template would contain two variables: {blog_title} and {blog_heading}</p>
127
128<p>There is no need to "echo" or do something with the data returned by <dfn>$this->parser->parse()</dfn>. It is automatically
129passed 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
130pass TRUE (boolean) to the third parameter:</p>
131
132<code>$string = $this->parser->parse('blog_template', $data, TRUE);</code>
133
134
135<h2>Variable Pairs</h2>
136
137<p>The above example code allows simple variables to be replaced. What if you would like an entire block of variables to be
138repeated, with each iteration containing new values? Consider the template example we showed at the top of the page:</p>
139
140<code>&lt;html&gt;<br />
141&lt;head&gt;<br />
142&lt;title&gt;<kbd>{blog_title}</kbd>&lt;/title&gt;<br />
143&lt;/head&gt;<br />
144&lt;body&gt;<br />
145<br />
146&lt;h3&gt;<kbd>{blog_heading}</kbd>&lt;/h3&gt;<br />
147<br />
148<kbd>{blog_entries}</kbd><br />
149&lt;h5&gt;<kbd>{title}</kbd>&lt;/h5&gt;<br />
150&lt;p&gt;<kbd>{body}</kbd>&lt;/p&gt;<br />
151<kbd>{/blog_entries}</kbd><br />
152
153&lt;/body&gt;<br />
154&lt;/html&gt;</code>
155
156<p>In the above code you'll notice a pair of variables: <kbd>{blog_entries}</kbd> data... <kbd>{/blog_entries}</kbd>.
157In a case like this, the entire chunk of data between these pairs would be repeated multiple times, corresponding
158to the number of rows in a result.</p>
159
160<p>Parsing variable pairs is done using the identical code shown above to parse single variables,
161except, you will add a multi-dimensional array corresponding to your variable pair data.
162Consider this example:</p>
163
164
165<code>$this->load->library('parser');<br />
166<br />
167$data = array(<br />
168&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title'&nbsp;&nbsp; => 'My Blog Title',<br />
169&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading',<br />
170&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_entries' => array(<br />
171&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 />
172&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 />
173&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 />
174&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 />
175&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 />
176&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 />
177&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
178<br />
179$this->parser->parse('blog_template', $data);</code>
180
181<p>If your "pair" data is coming from a database result, which is already a multi-dimensional array, you can simply
182use the database result function:</p>
183
184<code>
185$query = $this->db->query("SELECT * FROM blog");<br />
186<br />
187$this->load->library('parser');<br />
188<br />
189$data = array(<br />
190&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title'&nbsp;&nbsp; => 'My Blog Title',<br />
191&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading',<br />
192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_entries' => $query->result_array()<br />
193&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
194<br />
195$this->parser->parse('blog_template', $data);</code>
196
197
198
199
200</div>
201<!-- END CONTENT -->
202
203
204<div id="footer">
205<p>
206Previous Topic:&nbsp;&nbsp;<a href="trackback.html">Trackback Class</a>
207&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
208<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
209<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
210Next Topic:&nbsp;&nbsp;<a href="uri.html">URI Class</a>
211<p>
212<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
213</div>
214
215</body>
216</html>