blob: d6279f9c292329ce7449902add2deef13b931c97 [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>
admin4979a122006-10-11 19:40:07 +000037<td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
adminb0dd10f2006-08-25 17:25:49 +000038</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
adminb0dd10f2006-08-25 17:25:49 +0000107
108<p>The following functions are available in this library:</p>
109
110<h2>$this->parser->parse()</h2>
111
112<p>This variable accepts a template name and data array as input, and it generates a parsed version. Example:</p>
113
114<code>$this->load->library('parser');<br />
115<br />
116$data = array(<br />
117&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title' => 'My Blog Title',<br />
118&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading'<br />
119&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
120<br />
121$this->parser->parse('blog_template', $data);</code>
122
123<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),
124and the second parameter contains an associative array of data to be replaced in the template. In the above example, the
125template would contain two variables: {blog_title} and {blog_heading}</p>
126
127<p>There is no need to "echo" or do something with the data returned by <dfn>$this->parser->parse()</dfn>. It is automatically
128passed 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
129pass TRUE (boolean) to the third parameter:</p>
130
131<code>$string = $this->parser->parse('blog_template', $data, TRUE);</code>
132
133
134<h2>Variable Pairs</h2>
135
136<p>The above example code allows simple variables to be replaced. What if you would like an entire block of variables to be
137repeated, with each iteration containing new values? Consider the template example we showed at the top of the page:</p>
138
139<code>&lt;html&gt;<br />
140&lt;head&gt;<br />
141&lt;title&gt;<kbd>{blog_title}</kbd>&lt;/title&gt;<br />
142&lt;/head&gt;<br />
143&lt;body&gt;<br />
144<br />
145&lt;h3&gt;<kbd>{blog_heading}</kbd>&lt;/h3&gt;<br />
146<br />
147<kbd>{blog_entries}</kbd><br />
148&lt;h5&gt;<kbd>{title}</kbd>&lt;/h5&gt;<br />
149&lt;p&gt;<kbd>{body}</kbd>&lt;/p&gt;<br />
150<kbd>{/blog_entries}</kbd><br />
151
152&lt;/body&gt;<br />
153&lt;/html&gt;</code>
154
155<p>In the above code you'll notice a pair of variables: <kbd>{blog_entries}</kbd> data... <kbd>{/blog_entries}</kbd>.
156In a case like this, the entire chunk of data between these pairs would be repeated multiple times, corresponding
157to the number of rows in a result.</p>
158
159<p>Parsing variable pairs is done using the identical code shown above to parse single variables,
160except, you will add a multi-dimensional array corresponding to your variable pair data.
161Consider this example:</p>
162
163
164<code>$this->load->library('parser');<br />
165<br />
166$data = array(<br />
167&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title'&nbsp;&nbsp; => 'My Blog Title',<br />
168&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading',<br />
169&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_entries' => array(<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;array('title' => 'Title 1', 'body' => 'Body 1'),<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 2', 'body' => 'Body 2'),<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 3', 'body' => 'Body 3'),<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 4', 'body' => 'Body 4'),<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 5', 'body' => 'Body 5')<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;)<br />
176&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
177<br />
178$this->parser->parse('blog_template', $data);</code>
179
180<p>If your "pair" data is coming from a database result, which is already a multi-dimensional array, you can simply
181use the database result function:</p>
182
183<code>
184$query = $this->db->query("SELECT * FROM blog");<br />
185<br />
186$this->load->library('parser');<br />
187<br />
188$data = array(<br />
189&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_title'&nbsp;&nbsp; => 'My Blog Title',<br />
190&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_heading' => 'My Blog Heading',<br />
191&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_entries' => $query->result_array()<br />
192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
193<br />
194$this->parser->parse('blog_template', $data);</code>
195
196
197
198
199</div>
200<!-- END CONTENT -->
201
202
203<div id="footer">
204<p>
205Previous Topic:&nbsp;&nbsp;<a href="trackback.html">Trackback Class</a>
206&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
207<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
208<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
209Next Topic:&nbsp;&nbsp;<a href="uri.html">URI Class</a>
210<p>
211<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>
212</div>
213
214</body>
215</html>