blob: 0b77ae4b9da9e3e02e308602f68b7b61ea8ba6f9 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#####################
2Template Parser Class
3#####################
4
5The Template Parser Class enables you to parse pseudo-variables
6contained within your view files. It can parse simple variables or
7variable tag pairs. If you've never used a template engine,
8pseudo-variables look like this::
9
Derek Joneseb946d02011-10-05 15:47:43 -050010 <html>
11 <head>
12 <title>{blog_title}</title>
13 </head>
14 <body>
15
16 <h3>{blog_heading}</h3>
17
18 {blog_entries}
19 <h5>{title}</h5>
20 <p>{body}</p>
21 {/blog_entries}
22 </body>
23 </html>
Derek Jones8ede1a22011-10-05 13:34:52 -050024
25These variables are not actual PHP variables, but rather plain text
26representations that allow you to eliminate PHP from your templates
27(view files).
28
29.. note:: CodeIgniter does **not** require you to use this class since
30 using pure PHP in your view pages lets them run a little faster.
31 However, some developers prefer to use a template engine if they work
32 with designers who they feel would find some confusion working with PHP.
33
34.. important:: The Template Parser Class is **not** a full-blown
35 template parsing solution. We've kept it very lean on purpose in order
36 to maintain maximum performance.
37
38Initializing the Class
39======================
40
41Like most other classes in CodeIgniter, the Parser class is initialized
42in your controller using the $this->load->library function::
43
44 $this->load->library('parser');
45
46Once loaded, the Parser library object will be available using:
47$this->parser
48
49The following functions are available in this library:
50
51$this->parser->parse()
52======================
53
54This method accepts a template name and data array as input, and it
55generates a parsed version. Example::
56
Derek Joneseb946d02011-10-05 15:47:43 -050057 $this->load->library('parser');
58
59 $data = array(
60 'blog_title' => 'My Blog Title',
61 'blog_heading' => 'My Blog Heading'
62 );
63
64 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050065
66The first parameter contains the name of the :doc:`view
67file <../general/views>` (in this example the file would be called
68blog_template.php), and the second parameter contains an associative
69array of data to be replaced in the template. In the above example, the
70template would contain two variables: {blog_title} and {blog_heading}
71
72There is no need to "echo" or do something with the data returned by
73$this->parser->parse(). It is automatically passed to the output class
74to be sent to the browser. However, if you do want the data returned
75instead of sent to the output class you can pass TRUE (boolean) to the
76third parameter::
77
78 $string = $this->parser->parse('blog_template', $data, TRUE);
79
80$this->parser->parse_string()
81==============================
82
83This method works exactly like parse(), only accepts a string as the
84first parameter in place of a view file.
85
86Variable Pairs
87==============
88
89The above example code allows simple variables to be replaced. What if
90you would like an entire block of variables to be repeated, with each
91iteration containing new values? Consider the template example we showed
92at the top of the page::
93
Derek Joneseb946d02011-10-05 15:47:43 -050094 <html>
95 <head>
96 <title>{blog_title}</title>
97 </head>
98 <body>
99
100 <h3>{blog_heading}</h3>
101
102 {blog_entries}
103 <h5>{title}</h5>
104 <p>{body}</p>
105 {/blog_entries}
106 </body>
107 </html>
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
109In the above code you'll notice a pair of variables: {blog_entries}
110data... {/blog_entries}. In a case like this, the entire chunk of data
111between these pairs would be repeated multiple times, corresponding to
112the number of rows in a result.
113
114Parsing variable pairs is done using the identical code shown above to
115parse single variables, except, you will add a multi-dimensional array
116corresponding to your variable pair data. Consider this example::
117
Derek Joneseb946d02011-10-05 15:47:43 -0500118 $this->load->library('parser');
119
120 $data = array(
121 'blog_title' => 'My Blog Title',
122 'blog_heading' => 'My Blog Heading',
123 'blog_entries' => array(
124 array('title' => 'Title 1', 'body' => 'Body 1'),
125 array('title' => 'Title 2', 'body' => 'Body 2'),
126 array('title' => 'Title 3', 'body' => 'Body 3'),
127 array('title' => 'Title 4', 'body' => 'Body 4'),
128 array('title' => 'Title 5', 'body' => 'Body 5')
129 )
130 );
131
132 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
134If your "pair" data is coming from a database result, which is already a
135multi-dimensional array, you can simply use the database result_array()
136function::
137
Derek Joneseb946d02011-10-05 15:47:43 -0500138 $query = $this->db->query("SELECT * FROM blog");
139
140 $this->load->library('parser');
141
142 $data = array(
143 'blog_title' => 'My Blog Title',
144 'blog_heading' => 'My Blog Heading',
145 'blog_entries' => $query->result_array()
146 );
147
148 $this->parser->parse('blog_template', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500149