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">
|
| 2 | <html>
|
| 3 | <head>
|
| 4 |
|
| 5 | <title>Code Igniter User Guide</title>
|
| 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 |
|
| 10 | <script type="text/javascript" src="../scripts/nav.js"></script>
|
| 11 | <script type="text/javascript" src="../scripts/prototype.lite.js"></script>
|
| 12 | <script type="text/javascript" src="../scripts/moo.fx.js"></script>
|
| 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
| 15 | myHeight = new fx.Height('nav', {duration: 400});
|
| 16 | myHeight.hide();
|
| 17 | }
|
| 18 | </script>
|
| 19 |
|
| 20 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 21 | <meta http-equiv='expires' content='-1' />
|
| 22 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 23 | <meta name='robots' content='all' />
|
| 24 | <meta name='author' content='Rick Ellis' />
|
| 25 | <meta name='description' content='Code Igniter User Guide' />
|
| 26 |
|
| 27 | </head>
|
| 28 | <body>
|
| 29 |
|
| 30 | <!-- START NAVIGATION -->
|
| 31 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 32 | <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>
|
| 33 | <div id="masthead">
|
| 34 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 35 | <tr>
|
admin | 9fc347d | 2006-09-21 00:00:28 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
|
| 38 | </tr>
|
| 39 | </table>
|
| 40 | </div>
|
| 41 | <!-- END NAVIGATION -->
|
| 42 |
|
| 43 |
|
| 44 | <!-- START BREADCRUMB -->
|
| 45 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 46 | <tr>
|
| 47 | <td id="breadcrumb">
|
| 48 | <a href="http://www.codeigniter.com/">Code Igniter Home</a> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Template Parser Class
|
| 51 | </td>
|
| 52 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
|
| 53 | </tr>
|
| 54 | </table>
|
| 55 | <!-- END BREADCRUMB -->
|
| 56 |
|
| 57 | <br clear="all" />
|
| 58 |
|
| 59 |
|
| 60 | <!-- START CONTENT -->
|
| 61 | <div id="content">
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 | <h1>Template Parser Class</h1>
|
| 67 |
|
| 68 | <p>The Template Parser Class enables you to parse pseudo-variables contained within your view files. It can parse simple
|
| 69 | variables or variable tag pairs. If you've never used a template engine, pseudo-variables look like this:</p>
|
| 70 |
|
| 71 | <code><html><br />
|
| 72 | <head><br />
|
| 73 | <title><kbd>{blog_title}</kbd></title><br />
|
| 74 | </head><br />
|
| 75 | <body><br />
|
| 76 | <br />
|
| 77 | <h3><kbd>{blog_heading}</kbd></h3><br />
|
| 78 | <br />
|
| 79 | <kbd>{blog_entries}</kbd><br />
|
| 80 | <h5><kbd>{title}</kbd></h5><br />
|
| 81 | <p><kbd>{body}</kbd></p><br />
|
| 82 | <kbd>{/blog_entries}</kbd><br />
|
| 83 |
|
| 84 | </body><br />
|
| 85 | </html></code>
|
| 86 |
|
| 87 | <p>These variables are not actual PHP variables, but rather plain text representations that allow you to eliminate
|
| 88 | PHP from your templates (view files).</p>
|
| 89 |
|
| 90 | <p class="important"><strong>Note:</strong> Code Igniter does <strong>not</strong> require you to use this class
|
| 91 | since using pure PHP in your view pages lets them run a little faster. However, some developers prefer to use a template engine if
|
| 92 | they work with designers who they feel would find some confusion working with PHP.</p>
|
| 93 |
|
| 94 | <p><strong>Also Note:</strong> The Template Parser Class is <strong>not</strong> not a
|
| 95 | full-blown template parsing solution. We've kept it very lean on purpose in order to maintain maximum performance.
|
| 96 |
|
| 97 | </p>
|
| 98 |
|
| 99 |
|
| 100 | <h2>Initializing the Class</h2>
|
| 101 |
|
| 102 | <p>Like most other classes in Code Igniter, the Parser class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
|
| 103 |
|
| 104 | <code>$this->load->library('parser');</code>
|
| 105 | <p>Once loaded, the Parser library object will be available using: <dfn>$this->parser</dfn></p>
|
| 106 |
|
| 107 | <p>The following functions are available in this library:</p>
|
| 108 |
|
| 109 | <h2>$this->parser->parse()</h2>
|
| 110 |
|
| 111 | <p>This variable accepts a template name and data array as input, and it generates a parsed version. Example:</p>
|
| 112 |
|
| 113 | <code>$this->load->library('parser');<br />
|
| 114 | <br />
|
| 115 | $data = array(<br />
|
| 116 | 'blog_title' => 'My Blog Title',<br />
|
| 117 | 'blog_heading' => 'My Blog Heading'<br />
|
| 118 | );<br />
|
| 119 | <br />
|
| 120 | $this->parser->parse('blog_template', $data);</code>
|
| 121 |
|
| 122 | <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),
|
| 123 | and the second parameter contains an associative array of data to be replaced in the template. In the above example, the
|
| 124 | template would contain two variables: {blog_title} and {blog_heading}</p>
|
| 125 |
|
| 126 | <p>There is no need to "echo" or do something with the data returned by <dfn>$this->parser->parse()</dfn>. It is automatically
|
| 127 | 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
|
| 128 | pass TRUE (boolean) to the third parameter:</p>
|
| 129 |
|
| 130 | <code>$string = $this->parser->parse('blog_template', $data, TRUE);</code>
|
| 131 |
|
| 132 |
|
| 133 | <h2>Variable Pairs</h2>
|
| 134 |
|
| 135 | <p>The above example code allows simple variables to be replaced. What if you would like an entire block of variables to be
|
| 136 | repeated, with each iteration containing new values? Consider the template example we showed at the top of the page:</p>
|
| 137 |
|
| 138 | <code><html><br />
|
| 139 | <head><br />
|
| 140 | <title><kbd>{blog_title}</kbd></title><br />
|
| 141 | </head><br />
|
| 142 | <body><br />
|
| 143 | <br />
|
| 144 | <h3><kbd>{blog_heading}</kbd></h3><br />
|
| 145 | <br />
|
| 146 | <kbd>{blog_entries}</kbd><br />
|
| 147 | <h5><kbd>{title}</kbd></h5><br />
|
| 148 | <p><kbd>{body}</kbd></p><br />
|
| 149 | <kbd>{/blog_entries}</kbd><br />
|
| 150 |
|
| 151 | </body><br />
|
| 152 | </html></code>
|
| 153 |
|
| 154 | <p>In the above code you'll notice a pair of variables: <kbd>{blog_entries}</kbd> data... <kbd>{/blog_entries}</kbd>.
|
| 155 | In a case like this, the entire chunk of data between these pairs would be repeated multiple times, corresponding
|
| 156 | to the number of rows in a result.</p>
|
| 157 |
|
| 158 | <p>Parsing variable pairs is done using the identical code shown above to parse single variables,
|
| 159 | except, you will add a multi-dimensional array corresponding to your variable pair data.
|
| 160 | Consider this example:</p>
|
| 161 |
|
| 162 |
|
| 163 | <code>$this->load->library('parser');<br />
|
| 164 | <br />
|
| 165 | $data = array(<br />
|
| 166 | 'blog_title' => 'My Blog Title',<br />
|
| 167 | 'blog_heading' => 'My Blog Heading',<br />
|
| 168 | 'blog_entries' => array(<br />
|
| 169 | array('title' => 'Title 1', 'body' => 'Body 1'),<br />
|
| 170 | array('title' => 'Title 2', 'body' => 'Body 2'),<br />
|
| 171 | array('title' => 'Title 3', 'body' => 'Body 3'),<br />
|
| 172 | array('title' => 'Title 4', 'body' => 'Body 4'),<br />
|
| 173 | array('title' => 'Title 5', 'body' => 'Body 5')<br />
|
| 174 | )<br />
|
| 175 | );<br />
|
| 176 | <br />
|
| 177 | $this->parser->parse('blog_template', $data);</code>
|
| 178 |
|
| 179 | <p>If your "pair" data is coming from a database result, which is already a multi-dimensional array, you can simply
|
| 180 | use the database result function:</p>
|
| 181 |
|
| 182 | <code>
|
| 183 | $query = $this->db->query("SELECT * FROM blog");<br />
|
| 184 | <br />
|
| 185 | $this->load->library('parser');<br />
|
| 186 | <br />
|
| 187 | $data = array(<br />
|
| 188 | 'blog_title' => 'My Blog Title',<br />
|
| 189 | 'blog_heading' => 'My Blog Heading',<br />
|
| 190 | 'blog_entries' => $query->result_array()<br />
|
| 191 | );<br />
|
| 192 | <br />
|
| 193 | $this->parser->parse('blog_template', $data);</code>
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 | </div>
|
| 199 | <!-- END CONTENT -->
|
| 200 |
|
| 201 |
|
| 202 | <div id="footer">
|
| 203 | <p>
|
| 204 | Previous Topic: <a href="trackback.html">Trackback Class</a>
|
| 205 | ·
|
| 206 | <a href="#top">Top of Page</a> ·
|
| 207 | <a href="../index.html">User Guide Home</a> ·
|
| 208 | Next Topic: <a href="uri.html">URI Class</a>
|
| 209 | <p>
|
| 210 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 211 | </div>
|
| 212 |
|
| 213 | </body>
|
| 214 | </html> |