bzr branch
http://gegoxaren.bato24.eu/bzr/lenasys/trunk
20.1.1
by galaxyAbstractor
* Added an simple admin panel to the codeviewer-cmssy stuff |
1 |
<!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> |
|
31 |
<td><h1>CodeIgniter User Guide Version 2.1.3</h1></td> |
|
32 |
<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> › |
|
44 |
<a href="../index.html">User Guide Home</a> › |
|
45 |
Template 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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <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 |
|
64 |
variables or variable tag pairs. If you've never used a template engine, pseudo-variables look like this:</p> |
|
65 |
||
66 |
<code><html><br /> |
|
67 |
<head><br /> |
|
68 |
<title><kbd>{blog_title}</kbd></title><br /> |
|
69 |
</head><br /> |
|
70 |
<body><br /> |
|
71 |
<br /> |
|
72 |
<h3><kbd>{blog_heading}</kbd></h3><br /> |
|
73 |
<br /> |
|
74 |
<kbd>{blog_entries}</kbd><br /> |
|
75 |
<h5><kbd>{title}</kbd></h5><br /> |
|
76 |
<p><kbd>{body}</kbd></p><br /> |
|
77 |
<kbd>{/blog_entries}</kbd><br /> |
|
78 |
||
79 |
</body><br /> |
|
80 |
</html></code> |
|
81 |
||
82 |
<p>These variables are not actual PHP variables, but rather plain text representations that allow you to eliminate |
|
83 |
PHP 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 |
|
86 |
since using pure PHP in your view pages lets them run a little faster. However, some developers prefer to use a template engine if |
|
87 |
they 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 |
|
90 |
full-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 |
'blog_title' => 'My Blog Title',<br /> |
|
111 |
'blog_heading' => 'My Blog Heading'<br /> |
|
112 |
);<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), |
|
117 |
and the second parameter contains an associative array of data to be replaced in the template. In the above example, the |
|
118 |
template 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 |
|
121 |
passed 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 |
|
122 |
pass TRUE (boolean) to the third parameter:</p> |
|
123 |
||
124 |
<code>$string = $this->parser->parse('blog_template', $data, TRUE);</code> |
|
125 |
||
126 |
<h2>$this->parser->parse_string()</h2> |
|
127 |
||
128 |
<p>This method works exactly like parse(), only accepts a string as the first parameter in place of a view file.</p> |
|
129 |
||
130 |
||
131 |
<h2>Variable Pairs</h2> |
|
132 |
||
133 |
<p>The above example code allows simple variables to be replaced. What if you would like an entire block of variables to be |
|
134 |
repeated, with each iteration containing new values? Consider the template example we showed at the top of the page:</p> |
|
135 |
||
136 |
<code><html><br /> |
|
137 |
<head><br /> |
|
138 |
<title><kbd>{blog_title}</kbd></title><br /> |
|
139 |
</head><br /> |
|
140 |
<body><br /> |
|
141 |
<br /> |
|
142 |
<h3><kbd>{blog_heading}</kbd></h3><br /> |
|
143 |
<br /> |
|
144 |
<kbd>{blog_entries}</kbd><br /> |
|
145 |
<h5><kbd>{title}</kbd></h5><br /> |
|
146 |
<p><kbd>{body}</kbd></p><br /> |
|
147 |
<kbd>{/blog_entries}</kbd><br /> |
|
148 |
||
149 |
</body><br /> |
|
150 |
</html></code> |
|
151 |
||
152 |
<p>In the above code you'll notice a pair of variables: <kbd>{blog_entries}</kbd> data... <kbd>{/blog_entries}</kbd>. |
|
153 |
In a case like this, the entire chunk of data between these pairs would be repeated multiple times, corresponding |
|
154 |
to the number of rows in a result.</p> |
|
155 |
||
156 |
<p>Parsing variable pairs is done using the identical code shown above to parse single variables, |
|
157 |
except, you will add a multi-dimensional array corresponding to your variable pair data. |
|
158 |
Consider this example:</p> |
|
159 |
||
160 |
||
161 |
<code>$this->load->library('parser');<br /> |
|
162 |
<br /> |
|
163 |
$data = array(<br /> |
|
164 |
'blog_title' => 'My Blog Title',<br /> |
|
165 |
'blog_heading' => 'My Blog Heading',<br /> |
|
166 |
'blog_entries' => array(<br /> |
|
167 |
array('title' => 'Title 1', 'body' => 'Body 1'),<br /> |
|
168 |
array('title' => 'Title 2', 'body' => 'Body 2'),<br /> |
|
169 |
array('title' => 'Title 3', 'body' => 'Body 3'),<br /> |
|
170 |
array('title' => 'Title 4', 'body' => 'Body 4'),<br /> |
|
171 |
array('title' => 'Title 5', 'body' => 'Body 5')<br /> |
|
172 |
)<br /> |
|
173 |
);<br /> |
|
174 |
<br /> |
|
175 |
$this->parser->parse('blog_template', $data);</code> |
|
176 |
||
177 |
<p>If your "pair" data is coming from a database result, which is already a multi-dimensional array, you can simply |
|
178 |
use the database result_array() function:</p> |
|
179 |
||
180 |
<code> |
|
181 |
$query = $this->db->query("SELECT * FROM blog");<br /> |
|
182 |
<br /> |
|
183 |
$this->load->library('parser');<br /> |
|
184 |
<br /> |
|
185 |
$data = array(<br /> |
|
186 |
'blog_title' => 'My Blog Title',<br /> |
|
187 |
'blog_heading' => 'My Blog Heading',<br /> |
|
188 |
'blog_entries' => $query->result_array()<br /> |
|
189 |
);<br /> |
|
190 |
<br /> |
|
191 |
$this->parser->parse('blog_template', $data);</code> |
|
192 |
||
193 |
||
194 |
||
195 |
||
196 |
</div> |
|
197 |
<!-- END CONTENT -->
|
|
198 |
||
199 |
||
200 |
<div id="footer"> |
|
201 |
<p> |
|
202 |
Previous Topic: <a href="trackback.html">Trackback Class</a> |
|
203 |
·
|
|
204 |
<a href="#top">Top of Page</a> · |
|
205 |
<a href="../index.html">User Guide Home</a> · |
|
206 |
Next Topic: <a href="typography.html">Typography</a> |
|
207 |
</p> |
|
208 |
<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2012 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> |
|
209 |
</div> |
|
210 |
||
211 |
</body> |
|
212 |
</html> |