blob: e7c7e3abdfbf6f577d266354e63cbb836ed13f72 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#####################
2Template Parser Class
3#####################
4
James L Parrydef2c8f2014-11-24 10:56:45 -08005The Template Parser Class can perform simple text substitution for
6pseudo-variables contained within your view files.
7It can parse simple variables or variable tag pairs.
James L Parrydd73ea52014-11-24 09:51:33 -08008
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 Parrydef2c8f2014-11-24 10:56:45 -080013 <head>
14 <title>{blog_title}</title>
15 </head>
16 <body>
Derek Joneseb946d02011-10-05 15:47:43 -050017
James L Parrydef2c8f2014-11-24 10:56:45 -080018 <h3>{blog_heading}</h3>
Derek Joneseb946d02011-10-05 15:47:43 -050019
James L Parrydef2c8f2014-11-24 10:56:45 -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.
James L Parrydef2c8f2014-11-24 10:56:45 -080033 However, some developers prefer to use a template engine if
34 they work with designers who they feel would find some
35 confusion working with PHP.
Derek Jones8ede1a22011-10-05 13:34:52 -050036
37.. important:: The Template Parser Class is **not** a full-blown
38 template parsing solution. We've kept it very lean on purpose in order
39 to maintain maximum performance.
40
Andrey Andreevcc042092014-01-03 17:08:27 +020041.. contents::
42 :local:
43
44.. raw:: html
45
46 <div class="custom-index container"></div>
47
James L Parry307663f2014-11-24 10:59:09 -080048*******************************
49Using the Template Parser Class
50*******************************
51
Derek Jones8ede1a22011-10-05 13:34:52 -050052Initializing the Class
James L Parry307663f2014-11-24 10:59:09 -080053======================
Derek Jones8ede1a22011-10-05 13:34:52 -050054
55Like most other classes in CodeIgniter, the Parser class is initialized
56in your controller using the $this->load->library function::
57
58 $this->load->library('parser');
59
60Once loaded, the Parser library object will be available using:
61$this->parser
62
Andrey Andreev8da2e222014-01-03 12:47:37 +020063Parsing templates
James L Parry307663f2014-11-24 10:59:09 -080064=================
Derek Jones8ede1a22011-10-05 13:34:52 -050065
James L Parrydef2c8f2014-11-24 10:56:45 -080066You can use the ``parse()`` method to parse (or render) simple templates,
67like this::
Derek Joneseb946d02011-10-05 15:47:43 -050068
69 $data = array(
James L Parrydef2c8f2014-11-24 10:56:45 -080070 'blog_title' => 'My Blog Title',
71 'blog_heading' => 'My Blog Heading'
72 );
Derek Joneseb946d02011-10-05 15:47:43 -050073
74 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050075
76The first parameter contains the name of the :doc:`view
77file <../general/views>` (in this example the file would be called
78blog_template.php), and the second parameter contains an associative
79array of data to be replaced in the template. In the above example, the
80template would contain two variables: {blog_title} and {blog_heading}
81
82There is no need to "echo" or do something with the data returned by
83$this->parser->parse(). It is automatically passed to the output class
84to be sent to the browser. However, if you do want the data returned
James L Parrydd73ea52014-11-24 09:51:33 -080085instead of sent to the output class you can pass TRUE (boolean) as the
Derek Jones8ede1a22011-10-05 13:34:52 -050086third parameter::
87
88 $string = $this->parser->parse('blog_template', $data, TRUE);
89
Derek Jones8ede1a22011-10-05 13:34:52 -050090Variable Pairs
James L Parry307663f2014-11-24 10:59:09 -080091==============
Derek Jones8ede1a22011-10-05 13:34:52 -050092
93The above example code allows simple variables to be replaced. What if
94you would like an entire block of variables to be repeated, with each
95iteration containing new values? Consider the template example we showed
96at the top of the page::
97
Derek Joneseb946d02011-10-05 15:47:43 -050098 <html>
James L Parrydef2c8f2014-11-24 10:56:45 -080099 <head>
100 <title>{blog_title}</title>
101 </head>
102 <body>
Derek Joneseb946d02011-10-05 15:47:43 -0500103
James L Parrydef2c8f2014-11-24 10:56:45 -0800104 <h3>{blog_heading}</h3>
Derek Joneseb946d02011-10-05 15:47:43 -0500105
James L Parrydef2c8f2014-11-24 10:56:45 -0800106 {blog_entries}
107 <h5>{title}</h5>
108 <p>{body}</p>
109 {/blog_entries}
110 </body>
Derek Joneseb946d02011-10-05 15:47:43 -0500111 </html>
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
113In the above code you'll notice a pair of variables: {blog_entries}
114data... {/blog_entries}. In a case like this, the entire chunk of data
115between these pairs would be repeated multiple times, corresponding to
James L Parrydd73ea52014-11-24 09:51:33 -0800116the number of rows in the "blog_entries" element of the parameters array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
118Parsing variable pairs is done using the identical code shown above to
119parse single variables, except, you will add a multi-dimensional array
120corresponding to your variable pair data. Consider this example::
121
Derek Joneseb946d02011-10-05 15:47:43 -0500122 $this->load->library('parser');
123
124 $data = array(
James L Parrydef2c8f2014-11-24 10:56:45 -0800125 'blog_title' => 'My Blog Title',
126 'blog_heading' => 'My Blog Heading',
127 'blog_entries' => array(
128 array('title' => 'Title 1', 'body' => 'Body 1'),
129 array('title' => 'Title 2', 'body' => 'Body 2'),
130 array('title' => 'Title 3', 'body' => 'Body 3'),
131 array('title' => 'Title 4', 'body' => 'Body 4'),
132 array('title' => 'Title 5', 'body' => 'Body 5')
133 )
134 );
Derek Joneseb946d02011-10-05 15:47:43 -0500135
136 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500137
138If your "pair" data is coming from a database result, which is already a
139multi-dimensional array, you can simply use the database result_array()
140function::
141
Derek Joneseb946d02011-10-05 15:47:43 -0500142 $query = $this->db->query("SELECT * FROM blog");
143
144 $this->load->library('parser');
145
146 $data = array(
James L Parrydef2c8f2014-11-24 10:56:45 -0800147 'blog_title' => 'My Blog Title',
148 'blog_heading' => 'My Blog Heading',
149 'blog_entries' => $query->result_array()
150 );
Derek Joneseb946d02011-10-05 15:47:43 -0500151
152 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
James L Parry307663f2014-11-24 10:59:09 -0800154Usage Notes
155===========
James L Parrydd73ea52014-11-24 09:51:33 -0800156
James L Parrydef2c8f2014-11-24 10:56:45 -0800157If you include substitution parameters that are not referenced in your
158template, they are ignored::
James L Parrydd73ea52014-11-24 09:51:33 -0800159
160 $template = 'Hello, {firstname} {lastname}';
161 $data = array(
162 'title' => 'Mr',
163 'firstname' => 'John',
164 'lastname' => 'Doe'
165 );
166 $this->parser->parse_string($template, $data);
167
168 Result: Hello, John Doe
169
James L Parrydef2c8f2014-11-24 10:56:45 -0800170If you do not include a substitution parameter that is referenced in your
171template, the original pseudo-variable is shown in the result::
James L Parrydd73ea52014-11-24 09:51:33 -0800172
173 $template = 'Hello, {firstname} {initials} {lastname}';
174 $data = array(
175 'title' => 'Mr',
176 'firstname' => 'John',
177 'lastname' => 'Doe'
178 );
179 $this->parser->parse_string($template, $data);
180
181 Result: Hello, John {initials} Doe
182
James L Parrydef2c8f2014-11-24 10:56:45 -0800183If you provide a string substitution parameter when an array is expected,
184i.e. for a variable pair, the substitution is done for the opening variable
185pair tag, but the closing variable pair tag is not rendered properly::
James L Parrydd73ea52014-11-24 09:51:33 -0800186
187 $template = 'Hello, {firstname} {lastname} ({degrees}{degree} {/degrees})';
188 $data = array(
189 'degrees' => 'Mr',
190 'firstname' => 'John',
191 'lastname' => 'Doe',
192 'titles' => array(
193 array('degree' => 'BSc'),
194 array('degree' => 'PhD')
195
196 )
197 );
198 $this->parser->parse_string($template, $data);
199
200 Result: Hello, John Doe (Mr{degree} {/degrees})
201
James L Parrydef2c8f2014-11-24 10:56:45 -0800202If you name one of your individual substitution parameters the same as one
203used inside a variable pair, the results
James L Parrydd73ea52014-11-24 09:51:33 -0800204may not be as expected::
205
206 $template = 'Hello, {firstname} {lastname} ({degrees}{degree} {/degrees})';
207 $data = array(
208 'degree' => 'Mr',
209 'firstname' => 'John',
210 'lastname' => 'Doe',
211 'degrees' => array(
212 array('degree' => 'BSc'),
213 array('degree' => 'PhD')
214
215 )
216 );
217 $this->parser->parse_string($template, $data);
218
219 Result: Hello, John Doe (Mr Mr )
220
James L Parrydd73ea52014-11-24 09:51:33 -0800221View Fragments
James L Parry307663f2014-11-24 10:59:09 -0800222==============
James L Parrydd73ea52014-11-24 09:51:33 -0800223
James L Parrydef2c8f2014-11-24 10:56:45 -0800224You do not have to use variable pairs to get the effect of iteration in
225your views. It is possible to use a view fragment for what would be inside
226a variable pair, and to control the iteration in your controller instead
227of in the view.
James L Parrydd73ea52014-11-24 09:51:33 -0800228
229An example with the iteration controlled in the view::
230
James L Parrydef2c8f2014-11-24 10:56:45 -0800231 $template = '<ul>{menuitems}
232 <li><a href="{link}">{title}</a></li>
233 {/menuitems}</ul>';
James L Parrydd73ea52014-11-24 09:51:33 -0800234 $data = array(
235 'menuitems' => array(
236 array('title' => 'First Link', 'link' => '/first'),
237 array('title' => 'Second Link', 'link' => '/second'),
238 )
239 );
240 $this->parser->parse_string($template, $data);
241
242 Result:
243 - First Link
244 - Second Link
245
James L Parrydef2c8f2014-11-24 10:56:45 -0800246An example with the iteration controlled in the controller,
247using a view fragment::
James L Parrydd73ea52014-11-24 09:51:33 -0800248
James L Parrydef2c8f2014-11-24 10:56:45 -0800249 $temp = '';
James L Parrydd73ea52014-11-24 09:51:33 -0800250 $template1 = '<li><a href="{link}">{title}</a></li>';
251 $data1 = array(
252 array('title' => 'First Link', 'link' => '/first'),
253 array('title' => 'Second Link', 'link' => '/second'),
254 );
255 foreach ($data1 as $menuitem) {
James L Parrydef2c8f2014-11-24 10:56:45 -0800256 $temp .= $this->parser->parse_string($template1, $menuitem, TRUE);
James L Parrydd73ea52014-11-24 09:51:33 -0800257 }
258
259 $template = '<ul>{menuitems}</ul>';
260 $data = array(
James L Parrydef2c8f2014-11-24 10:56:45 -0800261 'menuitems' => $temp
James L Parrydd73ea52014-11-24 09:51:33 -0800262 );
263 $this->parser->parse_string($template, $data);
264
265 Result:
266 - First Link
267 - Second Link
268
Andrey Andreev8da2e222014-01-03 12:47:37 +0200269***************
270Class Reference
271***************
272
Andrey Andreev1e584202014-02-07 21:50:36 +0200273.. class:: CI_Parser
Andrey Andreev8da2e222014-01-03 12:47:37 +0200274
275 .. method:: parse($template, $data[, $return = FALSE])
276
Andrey Andreev28c2c972014-02-08 04:27:48 +0200277 :param string $template: Path to view file
278 :param array $data: Variable data
279 :param bool $return: Whether to only return the parsed template
280 :returns: Parsed template string
281 :rtype: string
Andrey Andreev8da2e222014-01-03 12:47:37 +0200282
283 Parses a template from the provided path and variables.
284
285 .. method:: parse_string($template, $data[, $return = FALSE])
286
Andrey Andreev28c2c972014-02-08 04:27:48 +0200287 :param string $template: Path to view file
288 :param array $data: Variable data
289 :param bool $return: Whether to only return the parsed template
290 :returns: Parsed template string
291 :rtype: string
Andrey Andreev8da2e222014-01-03 12:47:37 +0200292
James L Parrydef2c8f2014-11-24 10:56:45 -0800293 This method works exactly like ``parse()``, only it accepts
294 the template as a string instead of loading a view file.
Andrey Andreev8da2e222014-01-03 12:47:37 +0200295
296 .. method:: set_delimiters([$l = '{'[, $r = '}']])
297
Andrey Andreev28c2c972014-02-08 04:27:48 +0200298 :param string $l: Left delimiter
299 :param string $r: Right delimiter
300 :rtype: void
Andrey Andreev8da2e222014-01-03 12:47:37 +0200301
James L Parrydef2c8f2014-11-24 10:56:45 -0800302 Sets the delimiters (opening and closing) for a
303 pseudo-variable "tag" in a template.