blob: 23b2492c9e7f9ac615a57b7cf1bd89e5756c66cf [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#####################
2Template Parser Class
3#####################
4
James L Parrydd73ea52014-11-24 09:51:33 -08005The Template Parser Class can perform simple text substitution for pseudo-variables
Derek Jones8ede1a22011-10-05 13:34:52 -05006contained within your view files. It can parse simple variables or
James L Parrydd73ea52014-11-24 09:51:33 -08007variable tag pairs.
8
9If you've never used a template engine,
10pseudo-variable names are enclosed in braces, like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050011
Derek Joneseb946d02011-10-05 15:47:43 -050012 <html>
James L Parrydd73ea52014-11-24 09:51:33 -080013 <head>
14 <title>{blog_title}</title>
15 </head>
16 <body>
Derek Joneseb946d02011-10-05 15:47:43 -050017
James L Parrydd73ea52014-11-24 09:51:33 -080018 <h3>{blog_heading}</h3>
Derek Joneseb946d02011-10-05 15:47:43 -050019
James L Parrydd73ea52014-11-24 09:51:33 -080020 {blog_entries}
21 <h5>{title}</h5>
22 <p>{body}</p>
23 {/blog_entries}
24 </body>
Derek Joneseb946d02011-10-05 15:47:43 -050025 </html>
Derek Jones8ede1a22011-10-05 13:34:52 -050026
27These variables are not actual PHP variables, but rather plain text
28representations that allow you to eliminate PHP from your templates
29(view files).
30
31.. note:: CodeIgniter does **not** require you to use this class since
32 using pure PHP in your view pages lets them run a little faster.
33 However, some developers prefer to use a template engine if they work
34 with designers who they feel would find some confusion working with PHP.
35
36.. important:: The Template Parser Class is **not** a full-blown
37 template parsing solution. We've kept it very lean on purpose in order
38 to maintain maximum performance.
39
Andrey Andreevcc042092014-01-03 17:08:27 +020040.. contents::
41 :local:
42
43.. raw:: html
44
45 <div class="custom-index container"></div>
46
James L Parrydd73ea52014-11-24 09:51:33 -080047**********************
Derek Jones8ede1a22011-10-05 13:34:52 -050048Initializing the Class
James L Parrydd73ea52014-11-24 09:51:33 -080049**********************
Derek Jones8ede1a22011-10-05 13:34:52 -050050
51Like most other classes in CodeIgniter, the Parser class is initialized
52in your controller using the $this->load->library function::
53
54 $this->load->library('parser');
55
56Once loaded, the Parser library object will be available using:
57$this->parser
58
James L Parrydd73ea52014-11-24 09:51:33 -080059*****************
Andrey Andreev8da2e222014-01-03 12:47:37 +020060Parsing templates
James L Parrydd73ea52014-11-24 09:51:33 -080061*****************
Derek Jones8ede1a22011-10-05 13:34:52 -050062
Andrey Andreev8da2e222014-01-03 12:47:37 +020063You can use the ``parse()`` method to parse (or render) simple templates, like this::
Derek Joneseb946d02011-10-05 15:47:43 -050064
65 $data = array(
66 'blog_title' => 'My Blog Title',
67 'blog_heading' => 'My Blog Heading'
68 );
69
70 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050071
72The first parameter contains the name of the :doc:`view
73file <../general/views>` (in this example the file would be called
74blog_template.php), and the second parameter contains an associative
75array of data to be replaced in the template. In the above example, the
76template would contain two variables: {blog_title} and {blog_heading}
77
78There is no need to "echo" or do something with the data returned by
79$this->parser->parse(). It is automatically passed to the output class
80to be sent to the browser. However, if you do want the data returned
James L Parrydd73ea52014-11-24 09:51:33 -080081instead of sent to the output class you can pass TRUE (boolean) as the
Derek Jones8ede1a22011-10-05 13:34:52 -050082third parameter::
83
84 $string = $this->parser->parse('blog_template', $data, TRUE);
85
James L Parrydd73ea52014-11-24 09:51:33 -080086**************
Derek Jones8ede1a22011-10-05 13:34:52 -050087Variable Pairs
James L Parrydd73ea52014-11-24 09:51:33 -080088**************
Derek Jones8ede1a22011-10-05 13:34:52 -050089
90The above example code allows simple variables to be replaced. What if
91you would like an entire block of variables to be repeated, with each
92iteration containing new values? Consider the template example we showed
93at the top of the page::
94
Derek Joneseb946d02011-10-05 15:47:43 -050095 <html>
James L Parrydd73ea52014-11-24 09:51:33 -080096 <head>
97 <title>{blog_title}</title>
98 </head>
99 <body>
Derek Joneseb946d02011-10-05 15:47:43 -0500100
James L Parrydd73ea52014-11-24 09:51:33 -0800101 <h3>{blog_heading}</h3>
Derek Joneseb946d02011-10-05 15:47:43 -0500102
James L Parrydd73ea52014-11-24 09:51:33 -0800103 {blog_entries}
104 <h5>{title}</h5>
105 <p>{body}</p>
106 {/blog_entries}
107 </body>
Derek Joneseb946d02011-10-05 15:47:43 -0500108 </html>
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
110In the above code you'll notice a pair of variables: {blog_entries}
111data... {/blog_entries}. In a case like this, the entire chunk of data
112between these pairs would be repeated multiple times, corresponding to
James L Parrydd73ea52014-11-24 09:51:33 -0800113the number of rows in the "blog_entries" element of the parameters array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
115Parsing variable pairs is done using the identical code shown above to
116parse single variables, except, you will add a multi-dimensional array
117corresponding to your variable pair data. Consider this example::
118
Derek Joneseb946d02011-10-05 15:47:43 -0500119 $this->load->library('parser');
120
121 $data = array(
122 'blog_title' => 'My Blog Title',
123 'blog_heading' => 'My Blog Heading',
124 'blog_entries' => array(
125 array('title' => 'Title 1', 'body' => 'Body 1'),
126 array('title' => 'Title 2', 'body' => 'Body 2'),
127 array('title' => 'Title 3', 'body' => 'Body 3'),
128 array('title' => 'Title 4', 'body' => 'Body 4'),
129 array('title' => 'Title 5', 'body' => 'Body 5')
130 )
131 );
132
133 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500134
135If your "pair" data is coming from a database result, which is already a
136multi-dimensional array, you can simply use the database result_array()
137function::
138
Derek Joneseb946d02011-10-05 15:47:43 -0500139 $query = $this->db->query("SELECT * FROM blog");
140
141 $this->load->library('parser');
142
143 $data = array(
144 'blog_title' => 'My Blog Title',
145 'blog_heading' => 'My Blog Heading',
146 'blog_entries' => $query->result_array()
147 );
148
149 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
James L Parrydd73ea52014-11-24 09:51:33 -0800151***************************
152Template Parser Usage Notes
153***************************
154
155If you include substitution parameters that are not referenced in your template, they are ignored::
156
157 $template = 'Hello, {firstname} {lastname}';
158 $data = array(
159 'title' => 'Mr',
160 'firstname' => 'John',
161 'lastname' => 'Doe'
162 );
163 $this->parser->parse_string($template, $data);
164
165 Result: Hello, John Doe
166
167If you do not include a substitution parameter that is referenced in your template, the original
168pseudo-variable is shown in the result::
169
170 $template = 'Hello, {firstname} {initials} {lastname}';
171 $data = array(
172 'title' => 'Mr',
173 'firstname' => 'John',
174 'lastname' => 'Doe'
175 );
176 $this->parser->parse_string($template, $data);
177
178 Result: Hello, John {initials} Doe
179
180If you provide a string substitution parameter when an array is expected, i.e. for a variable pair,
181the substitution is done for the opening variable pair tag, but the closing variable pair
182tag is not rendered properly::
183
184 $template = 'Hello, {firstname} {lastname} ({degrees}{degree} {/degrees})';
185 $data = array(
186 'degrees' => 'Mr',
187 'firstname' => 'John',
188 'lastname' => 'Doe',
189 'titles' => array(
190 array('degree' => 'BSc'),
191 array('degree' => 'PhD')
192
193 )
194 );
195 $this->parser->parse_string($template, $data);
196
197 Result: Hello, John Doe (Mr{degree} {/degrees})
198
199If you name one of your individual substitution parameters the same as one used inside a variable pair, the results
200may not be as expected::
201
202 $template = 'Hello, {firstname} {lastname} ({degrees}{degree} {/degrees})';
203 $data = array(
204 'degree' => 'Mr',
205 'firstname' => 'John',
206 'lastname' => 'Doe',
207 'degrees' => array(
208 array('degree' => 'BSc'),
209 array('degree' => 'PhD')
210
211 )
212 );
213 $this->parser->parse_string($template, $data);
214
215 Result: Hello, John Doe (Mr Mr )
216
217**************
218View Fragments
219**************
220
221You do not have to use variable pairs to get the effect of iteration in your views.
222It is possible to use a view fragment for what would be inside a variable pair, and to
223control the iteration in your controller instead of in the view.
224
225An example with the iteration controlled in the view::
226
227 $template = '<ul>{menuitems}<li><a href="{link}">{title}</a></li>{/menuitems}</ul>';
228 $data = array(
229 'menuitems' => array(
230 array('title' => 'First Link', 'link' => '/first'),
231 array('title' => 'Second Link', 'link' => '/second'),
232 )
233 );
234 $this->parser->parse_string($template, $data);
235
236 Result:
237 - First Link
238 - Second Link
239
240An example with the iteration controlled in the controller, using a view fragment::
241
242 $temp_result = '';
243 $template1 = '<li><a href="{link}">{title}</a></li>';
244 $data1 = array(
245 array('title' => 'First Link', 'link' => '/first'),
246 array('title' => 'Second Link', 'link' => '/second'),
247 );
248 foreach ($data1 as $menuitem) {
249 $temp_result .= $this->parser->parse_string($template1, $menuitem, TRUE);
250 }
251
252 $template = '<ul>{menuitems}</ul>';
253 $data = array(
254 'menuitems' => $temp_result
255 );
256 $this->parser->parse_string($template, $data);
257
258 Result:
259 - First Link
260 - Second Link
261
Andrey Andreev8da2e222014-01-03 12:47:37 +0200262***************
263Class Reference
264***************
265
Andrey Andreev1e584202014-02-07 21:50:36 +0200266.. class:: CI_Parser
Andrey Andreev8da2e222014-01-03 12:47:37 +0200267
268 .. method:: parse($template, $data[, $return = FALSE])
269
Andrey Andreev28c2c972014-02-08 04:27:48 +0200270 :param string $template: Path to view file
271 :param array $data: Variable data
272 :param bool $return: Whether to only return the parsed template
273 :returns: Parsed template string
274 :rtype: string
Andrey Andreev8da2e222014-01-03 12:47:37 +0200275
276 Parses a template from the provided path and variables.
277
278 .. method:: parse_string($template, $data[, $return = FALSE])
279
Andrey Andreev28c2c972014-02-08 04:27:48 +0200280 :param string $template: Path to view file
281 :param array $data: Variable data
282 :param bool $return: Whether to only return the parsed template
283 :returns: Parsed template string
284 :rtype: string
Andrey Andreev8da2e222014-01-03 12:47:37 +0200285
286 This method works exactly like ``parse()``, only it accepts the template as a
287 string instead of loading a view file.
288
289 .. method:: set_delimiters([$l = '{'[, $r = '}']])
290
Andrey Andreev28c2c972014-02-08 04:27:48 +0200291 :param string $l: Left delimiter
292 :param string $r: Right delimiter
293 :rtype: void
Andrey Andreev8da2e222014-01-03 12:47:37 +0200294
James L Parrydd73ea52014-11-24 09:51:33 -0800295 Sets the delimiters (opening and closing) for a pseudo-variable "tag" in a template.