admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
Derek Jones | fd93d22 | 2008-05-06 15:18:50 +0000 | [diff] [blame] | 5 | <title>Template Parser Class : CodeIgniter User Guide</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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.jpg" width="153" height="44" 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>
|
Derek Jones | 6b7b11f | 2008-05-06 16:07:27 +0000 | [diff] [blame^] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.2</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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">
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Template Parser Class
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 85 | <p class="important"><strong>Note:</strong> CodeIgniter does <strong>not</strong> require you to use this class
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 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
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 87 | they work with designers who they feel would find some confusion working with PHP.</p>
|
| 88 |
|
Derek Allard | 866b813 | 2008-04-21 22:02:37 +0000 | [diff] [blame] | 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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 91 |
|
| 92 |
|
| 93 | <h2>Initializing the Class</h2>
|
| 94 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
admin | b1fddc0 | 2006-10-09 21:29:07 +0000 | [diff] [blame] | 99 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 100 |
|
| 101 | <p>The following functions are available in this library:</p>
|
| 102 |
|
| 103 | <h2>$this->parser->parse()</h2>
|
| 104 |
|
Derek Allard | 866b813 | 2008-04-21 22:02:37 +0000 | [diff] [blame] | 105 | <p>This method accepts a template name and data array as input, and it generates a parsed version. Example:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 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
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 122 | pass TRUE (boolean) to the third parameter:</p>
|
| 123 |
|
| 124 | <code>$string = $this->parser->parse('blog_template', $data, TRUE);</code>
|
| 125 |
|
| 126 |
|
| 127 | <h2>Variable Pairs</h2>
|
| 128 |
|
| 129 | <p>The above example code allows simple variables to be replaced. What if you would like an entire block of variables to be
|
| 130 | repeated, with each iteration containing new values? Consider the template example we showed at the top of the page:</p>
|
| 131 |
|
| 132 | <code><html><br />
|
| 133 | <head><br />
|
| 134 | <title><kbd>{blog_title}</kbd></title><br />
|
| 135 | </head><br />
|
| 136 | <body><br />
|
| 137 | <br />
|
| 138 | <h3><kbd>{blog_heading}</kbd></h3><br />
|
| 139 | <br />
|
| 140 | <kbd>{blog_entries}</kbd><br />
|
| 141 | <h5><kbd>{title}</kbd></h5><br />
|
| 142 | <p><kbd>{body}</kbd></p><br />
|
| 143 | <kbd>{/blog_entries}</kbd><br />
|
| 144 |
|
| 145 | </body><br />
|
| 146 | </html></code>
|
| 147 |
|
| 148 | <p>In the above code you'll notice a pair of variables: <kbd>{blog_entries}</kbd> data... <kbd>{/blog_entries}</kbd>.
|
| 149 | In a case like this, the entire chunk of data between these pairs would be repeated multiple times, corresponding
|
| 150 | to the number of rows in a result.</p>
|
| 151 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 152 | <p>Parsing variable pairs is done using the identical code shown above to parse single variables,
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 153 | except, you will add a multi-dimensional array corresponding to your variable pair data.
|
| 154 | Consider this example:</p>
|
| 155 |
|
| 156 |
|
| 157 | <code>$this->load->library('parser');<br />
|
| 158 | <br />
|
| 159 | $data = array(<br />
|
| 160 | 'blog_title' => 'My Blog Title',<br />
|
| 161 | 'blog_heading' => 'My Blog Heading',<br />
|
| 162 | 'blog_entries' => array(<br />
|
| 163 | array('title' => 'Title 1', 'body' => 'Body 1'),<br />
|
| 164 | array('title' => 'Title 2', 'body' => 'Body 2'),<br />
|
| 165 | array('title' => 'Title 3', 'body' => 'Body 3'),<br />
|
| 166 | array('title' => 'Title 4', 'body' => 'Body 4'),<br />
|
| 167 | array('title' => 'Title 5', 'body' => 'Body 5')<br />
|
| 168 | )<br />
|
| 169 | );<br />
|
| 170 | <br />
|
| 171 | $this->parser->parse('blog_template', $data);</code>
|
| 172 |
|
| 173 | <p>If your "pair" data is coming from a database result, which is already a multi-dimensional array, you can simply
|
Derek Allard | 866b813 | 2008-04-21 22:02:37 +0000 | [diff] [blame] | 174 | use the database result_array() function:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 175 |
|
| 176 | <code>
|
| 177 | $query = $this->db->query("SELECT * FROM blog");<br />
|
| 178 | <br />
|
| 179 | $this->load->library('parser');<br />
|
| 180 | <br />
|
| 181 | $data = array(<br />
|
| 182 | 'blog_title' => 'My Blog Title',<br />
|
| 183 | 'blog_heading' => 'My Blog Heading',<br />
|
| 184 | 'blog_entries' => $query->result_array()<br />
|
| 185 | );<br />
|
| 186 | <br />
|
| 187 | $this->parser->parse('blog_template', $data);</code>
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 | </div>
|
| 193 | <!-- END CONTENT -->
|
| 194 |
|
| 195 |
|
| 196 | <div id="footer">
|
| 197 | <p>
|
| 198 | Previous Topic: <a href="trackback.html">Trackback Class</a>
|
| 199 | ·
|
| 200 | <a href="#top">Top of Page</a> ·
|
| 201 | <a href="../index.html">User Guide Home</a> ·
|
Derek Allard | 9da4dbc | 2007-04-03 11:39:35 +0000 | [diff] [blame] | 202 | Next Topic: <a href="unit_testing.html">Unit Testing Class</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 203 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 204 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 205 | </div>
|
| 206 |
|
| 207 | </body>
|
| 208 | </html> |