blob: 6c9d2895918fa95205506f42f477f73aa3b4a04e [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>
Andrey Andreevf3f06f22014-12-04 16:54:07 +020013 <head>
14 <title>{blog_title}</title>
15 </head>
16 <body>
17 <h3>{blog_heading}</h3>
Derek Joneseb946d02011-10-05 15:47:43 -050018
Andrey Andreevf3f06f22014-12-04 16:54:07 +020019 {blog_entries}
20 <h5>{title}</h5>
21 <p>{body}</p>
22 {/blog_entries}
Derek Joneseb946d02011-10-05 15:47:43 -050023
Andrey Andreevf3f06f22014-12-04 16:54:07 +020024 </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.
Andrey Andreevf3f06f22014-12-04 16:54:07 +020033 However, some developers prefer to use a template engine if
34 they work with designers who they feel would find some
James L Parrydef2c8f2014-11-24 10:56:45 -080035 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
Andrey Andreevf3f06f22014-12-04 16:54:07 +020056in your controller using the ``$this->load->library()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -050057
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
Andrey Andreevf3f06f22014-12-04 16:54:07 +020066You can use the ``parse()`` method to parse (or render) simple templates,
James L Parrydef2c8f2014-11-24 10:56:45 -080067like this::
Derek Joneseb946d02011-10-05 15:47:43 -050068
69 $data = array(
Andrey Andreevf3f06f22014-12-04 16:54:07 +020070 'blog_title' => 'My Blog Title',
71 'blog_heading' => 'My Blog Heading'
James L Parrydef2c8f2014-11-24 10:56:45 -080072 );
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>
Andrey Andreevf3f06f22014-12-04 16:54:07 +020099 <head>
100 <title>{blog_title}</title>
101 </head>
102 <body>
103 <h3>{blog_heading}</h3>
Derek Joneseb946d02011-10-05 15:47:43 -0500104
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200105 {blog_entries}
106 <h5>{title}</h5>
107 <p>{body}</p>
108 {/blog_entries}
Derek Joneseb946d02011-10-05 15:47:43 -0500109
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200110 </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(
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200125 '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 )
James L Parrydef2c8f2014-11-24 10:56:45 -0800134 );
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
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200139multi-dimensional array, you can simply use the database ``result_array()``
140method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
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(
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200147 'blog_title' => 'My Blog Title',
148 'blog_heading' => 'My Blog Heading',
149 'blog_entries' => $query->result_array()
James L Parrydef2c8f2014-11-24 10:56:45 -0800150 );
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
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200157If you include substitution parameters that are not referenced in your
James L Parrydef2c8f2014-11-24 10:56:45 -0800158template, they are ignored::
James L Parrydd73ea52014-11-24 09:51:33 -0800159
160 $template = 'Hello, {firstname} {lastname}';
161 $data = array(
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200162 'title' => 'Mr',
163 'firstname' => 'John',
164 'lastname' => 'Doe'
James L Parrydd73ea52014-11-24 09:51:33 -0800165 );
166 $this->parser->parse_string($template, $data);
167
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200168 // Result: Hello, John Doe
James L Parrydd73ea52014-11-24 09:51:33 -0800169
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200170If you do not include a substitution parameter that is referenced in your
James L Parrydef2c8f2014-11-24 10:56:45 -0800171template, 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(
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200175 'title' => 'Mr',
176 'firstname' => 'John',
177 'lastname' => 'Doe'
James L Parrydd73ea52014-11-24 09:51:33 -0800178 );
179 $this->parser->parse_string($template, $data);
180
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200181 // Result: Hello, John {initials} Doe
James L Parrydd73ea52014-11-24 09:51:33 -0800182
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200183If you provide a string substitution parameter when an array is expected,
James L Parrydef2c8f2014-11-24 10:56:45 -0800184i.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(
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200189 'degrees' => 'Mr',
190 'firstname' => 'John',
191 'lastname' => 'Doe',
192 'titles' => array(
193 array('degree' => 'BSc'),
194 array('degree' => 'PhD')
195 )
James L Parrydd73ea52014-11-24 09:51:33 -0800196 );
197 $this->parser->parse_string($template, $data);
198
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200199 // Result: Hello, John Doe (Mr{degree} {/degrees})
James L Parrydd73ea52014-11-24 09:51:33 -0800200
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200201If you name one of your individual substitution parameters the same as one
202used inside a variable pair, the results may not be as expected::
James L Parrydd73ea52014-11-24 09:51:33 -0800203
204 $template = 'Hello, {firstname} {lastname} ({degrees}{degree} {/degrees})';
205 $data = array(
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200206 'degree' => 'Mr',
207 'firstname' => 'John',
208 'lastname' => 'Doe',
209 'degrees' => array(
210 array('degree' => 'BSc'),
211 array('degree' => 'PhD')
212 )
James L Parrydd73ea52014-11-24 09:51:33 -0800213 );
214 $this->parser->parse_string($template, $data);
215
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200216 // Result: Hello, John Doe (Mr Mr )
James L Parrydd73ea52014-11-24 09:51:33 -0800217
James L Parrydd73ea52014-11-24 09:51:33 -0800218View Fragments
James L Parry307663f2014-11-24 10:59:09 -0800219==============
James L Parrydd73ea52014-11-24 09:51:33 -0800220
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200221You do not have to use variable pairs to get the effect of iteration in
222your views. It is possible to use a view fragment for what would be inside
223a variable pair, and to control the iteration in your controller instead
James L Parrydef2c8f2014-11-24 10:56:45 -0800224of in the view.
James L Parrydd73ea52014-11-24 09:51:33 -0800225
226An example with the iteration controlled in the view::
227
James L Parrydef2c8f2014-11-24 10:56:45 -0800228 $template = '<ul>{menuitems}
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200229 <li><a href="{link}">{title}</a></li>
230 {/menuitems}</ul>';
231
James L Parrydd73ea52014-11-24 09:51:33 -0800232 $data = array(
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200233 'menuitems' => array(
234 array('title' => 'First Link', 'link' => '/first'),
235 array('title' => 'Second Link', 'link' => '/second'),
236 )
James L Parrydd73ea52014-11-24 09:51:33 -0800237 );
238 $this->parser->parse_string($template, $data);
239
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200240Result::
241
242 <ul>
243 <li><a href="/first">First Link</a></li>
244 <li><a href="/second">Second Link</a></li>
245 </ul>
James L Parrydd73ea52014-11-24 09:51:33 -0800246
James L Parrydef2c8f2014-11-24 10:56:45 -0800247An example with the iteration controlled in the controller,
248using a view fragment::
James L Parrydd73ea52014-11-24 09:51:33 -0800249
James L Parrydef2c8f2014-11-24 10:56:45 -0800250 $temp = '';
James L Parrydd73ea52014-11-24 09:51:33 -0800251 $template1 = '<li><a href="{link}">{title}</a></li>';
252 $data1 = array(
253 array('title' => 'First Link', 'link' => '/first'),
254 array('title' => 'Second Link', 'link' => '/second'),
255 );
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200256
257 foreach ($data1 as $menuitem)
258 {
259 $temp .= $this->parser->parse_string($template1, $menuitem, TRUE);
James L Parrydd73ea52014-11-24 09:51:33 -0800260 }
261
262 $template = '<ul>{menuitems}</ul>';
263 $data = array(
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200264 'menuitems' => $temp
James L Parrydd73ea52014-11-24 09:51:33 -0800265 );
266 $this->parser->parse_string($template, $data);
267
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200268Result::
269
270 <ul>
271 <li><a href="/first">First Link</a></li>
272 <li><a href="/second">Second Link</a></li>
273 </ul>
James L Parrydd73ea52014-11-24 09:51:33 -0800274
Andrey Andreev8da2e222014-01-03 12:47:37 +0200275***************
276Class Reference
277***************
278
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200279.. php:class:: CI_Parser
Andrey Andreev8da2e222014-01-03 12:47:37 +0200280
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200281 .. php:method:: parse($template, $data[, $return = FALSE])
Andrey Andreev8da2e222014-01-03 12:47:37 +0200282
Andrey Andreev28c2c972014-02-08 04:27:48 +0200283 :param string $template: Path to view file
284 :param array $data: Variable data
285 :param bool $return: Whether to only return the parsed template
286 :returns: Parsed template string
287 :rtype: string
Andrey Andreev8da2e222014-01-03 12:47:37 +0200288
289 Parses a template from the provided path and variables.
290
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200291 .. php:method:: parse_string($template, $data[, $return = FALSE])
Andrey Andreev8da2e222014-01-03 12:47:37 +0200292
Andrey Andreev28c2c972014-02-08 04:27:48 +0200293 :param string $template: Path to view file
294 :param array $data: Variable data
295 :param bool $return: Whether to only return the parsed template
296 :returns: Parsed template string
297 :rtype: string
Andrey Andreev8da2e222014-01-03 12:47:37 +0200298
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200299 This method works exactly like ``parse()``, only it accepts
300 the template as a string instead of loading a view file.
Andrey Andreev8da2e222014-01-03 12:47:37 +0200301
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200302 .. php:method:: set_delimiters([$l = '{'[, $r = '}']])
Andrey Andreev8da2e222014-01-03 12:47:37 +0200303
Andrey Andreev28c2c972014-02-08 04:27:48 +0200304 :param string $l: Left delimiter
305 :param string $r: Right delimiter
306 :rtype: void
Andrey Andreev8da2e222014-01-03 12:47:37 +0200307
Andrey Andreevf3f06f22014-12-04 16:54:07 +0200308 Sets the delimiters (opening and closing) for a
309 pseudo-variable "tag" in a template.