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 Allard | 8039d4c | 2008-05-31 02:47:56 +0000 | [diff] [blame] | 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
Derek Jones | fd93d22 | 2008-05-06 15:18:50 +0000 | [diff] [blame] | 6 | <title>Views : CodeIgniter User Guide</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 7 |
|
| 8 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 9 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 10 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 15 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
Rick Ellis | 7cdef03 | 2008-08-26 18:44:54 +0000 | [diff] [blame] | 27 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 28 | <div id="masthead">
|
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 30 | <tr>
|
Rick Ellis | 2594953 | 2008-08-26 19:48:08 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.7</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 | Views
|
| 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 | <h1>Views</h1>
|
| 59 |
|
| 60 | <p>A <dfn>view</dfn> is simply a web page, or a page fragment, like a header, footer, sidebar, etc.
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 61 | In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 62 | of hierarchy.</p>
|
| 63 |
|
| 64 | <p>Views are never called directly, they must be loaded by a <a href="controllers.html">controller</a>. Remember that in an MVC framework, the Controller acts as the
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 65 | traffic cop, so it is responsible for fetching a particular view. If you have not read the <a href="controllers.html">Controllers</a> page
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 66 | you should do so before continuing.</p>
|
| 67 |
|
| 68 | <p>Using the example controller you created in the <a href="controllers.html">controller</a> page, let's add a view to it.</p>
|
| 69 |
|
| 70 | <h2>Creating a View</h2>
|
| 71 |
|
| 72 | <p>Using your text editor, create a file called <dfn>blogview.php</dfn>, and put this in it:</p>
|
| 73 |
|
| 74 | <textarea class="textarea" style="width:100%" cols="50" rows="10">
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 75 | <html>
|
| 76 | <head>
|
| 77 | <title>My Blog</title>
|
| 78 | </head>
|
| 79 | <body>
|
| 80 | <h1>Welcome to my Blog!</h1>
|
| 81 | </body>
|
| 82 | </html>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 83 | </textarea>
|
| 84 |
|
| 85 | <p>Then save the file in your <dfn>application/views/</dfn> folder.</p>
|
| 86 |
|
| 87 | <h2>Loading a View</h2>
|
| 88 |
|
| 89 | <p>To load a particular view file you will use the following function:</p>
|
| 90 |
|
| 91 | <code>$this->load->view('<var>name</var>');</code>
|
| 92 |
|
admin | 108d18a | 2006-09-02 17:45:38 +0000 | [diff] [blame] | 93 | <p>Where <var>name</var> is the name of your view file. Note: The .php file extension does not need to be specified unless you use something other then <kbd>.php</kbd>.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 94 |
|
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 95 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 96 | <p>Now, open the controller file you made earlier called <dfn>blog.php</dfn>, and replace the echo statement with the view loading function:</p>
|
| 97 |
|
| 98 |
|
| 99 | <textarea class="textarea" style="width:100%" cols="50" rows="10">
|
| 100 | <?php
|
| 101 | class Blog extends Controller {
|
| 102 |
|
| 103 | function index()
|
| 104 | {
|
| 105 | $this->load->view('blogview');
|
| 106 | }
|
| 107 | }
|
| 108 | ?>
|
| 109 | </textarea>
|
| 110 |
|
| 111 |
|
| 112 | <p>If you visit the your site using the URL you did earlier you should see your new view. The URL was similar to this:</p>
|
| 113 |
|
Derek Jones | ff845f9 | 2008-06-26 17:05:55 +0000 | [diff] [blame] | 114 | <code>example.com/index.php/<var>blog</var>/</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 115 |
|
Derek Allard | 270c0aa | 2008-01-18 23:19:41 +0000 | [diff] [blame] | 116 | <h2>Loading multiple views</h2>
|
| 117 | <p>CodeIgniter will intelligently handle multiple calls to $this->load->view from within a controller. If more then one call happens they will be appended together. For example, you may wish to have a header view, a menu view, a content view, and a footer view. That might look something like this:</p>
|
| 118 | <p><code><?php<br />
|
| 119 | <br />
|
| 120 | class Page extends Controller {<br /><br />
|
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 121 |
|
Derek Allard | 270c0aa | 2008-01-18 23:19:41 +0000 | [diff] [blame] | 122 | function index()<br />
|
| 123 | {<br />
|
| 124 | $data['page_title'] = 'Your title';<br />
|
| 125 | $this->load->view('header');<br />
|
| 126 | $this->load->view('menu');<br />
|
| 127 | $this->load->view('content', $data);<br />
|
| 128 | $this->load->view('footer');<br />
|
| 129 | }<br />
|
| 130 | <br />
|
| 131 | }<br />
|
Derek Allard | 928c55c | 2008-08-21 12:46:58 +0000 | [diff] [blame] | 132 | ?></code></p>
|
Derek Allard | 270c0aa | 2008-01-18 23:19:41 +0000 | [diff] [blame] | 133 | <p>In the example above, we are using "dynamically added data", which you will see below.</p>
|
| 134 | <h2>Storing Views within Sub-folders</h2>
|
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 135 | <p>Your view files can also be stored within sub-folders if you prefer that type of organization. When doing so you will need
|
| 136 | to include the folder name loading the view. Example:</p>
|
| 137 |
|
| 138 | <code>$this->load->view('<kbd>folder_name</kbd>/<var>file_name</var>');</code>
|
| 139 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 140 |
|
| 141 | <h2>Adding Dynamic Data to the View</h2>
|
| 142 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 143 | <p>Data is passed from the controller to the view by way of an <strong>array</strong> or an <strong>object</strong> in the second
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 144 | parameter of the view loading function. Here is an example using an array:</p>
|
| 145 |
|
| 146 | <code>$data = array(<br />
|
| 147 | 'title' => 'My Title',<br />
|
Rick Ellis | 0cb3404 | 2007-03-23 21:02:11 +0000 | [diff] [blame] | 148 | 'heading' => 'My Heading',<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 149 | 'message' => 'My Message'<br />
|
| 150 | );<br />
|
| 151 | <br />
|
| 152 | $this->load->view('blogview', <var>$data</var>);</code>
|
| 153 |
|
| 154 | <p>And here's an example using an object:</p>
|
| 155 |
|
| 156 | <code>$data = new Someclass();<br />
|
| 157 | $this->load->view('blogview', <var>$data</var>);</code>
|
| 158 |
|
| 159 | <p>Note: If you use an object, the class variables will be turned into array elements.</p>
|
| 160 |
|
| 161 |
|
| 162 | <p>Let's try it with your controller file. Open it add this code:</p>
|
| 163 |
|
| 164 | <textarea class="textarea" style="width:100%" cols="50" rows="14">
|
Derek Allard | 78c3fa8 | 2008-01-21 15:11:13 +0000 | [diff] [blame] | 165 | <?php
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 166 | class Blog extends Controller {
|
| 167 |
|
| 168 | function index()
|
| 169 | {
|
| 170 | $data['title'] = "My Real Title";
|
| 171 | $data['heading'] = "My Real Heading";
|
| 172 |
|
| 173 | $this->load->view('blogview', $data);
|
| 174 | }
|
| 175 | }
|
Derek Allard | 78c3fa8 | 2008-01-21 15:11:13 +0000 | [diff] [blame] | 176 | ?>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 177 | </textarea>
|
| 178 |
|
| 179 |
|
| 180 | <p>Now open your view file and change the text to variables that correspond to the array keys in your data:</p>
|
| 181 |
|
| 182 |
|
| 183 | <textarea class="textarea" style="width:100%" cols="50" rows="10">
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 184 | <html>
|
| 185 | <head>
|
| 186 | <title><?php echo $title;?></title>
|
| 187 | </head>
|
| 188 | <body>
|
| 189 | <h1><?php echo $heading;?></h1>
|
| 190 | </body>
|
| 191 | </html>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 192 | </textarea>
|
| 193 |
|
| 194 | <p>Then load the page at the URL you've been using and you should see the variables replaced.</p>
|
| 195 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 196 | <h2>Creating Loops</h2>
|
| 197 |
|
| 198 | <p>The data array you pass to your view files is not limited to simple variables. You can
|
| 199 | pass multi dimensional arrays, which can be looped to generate multiple rows. For example, if you
|
| 200 | pull data from your database it will typically be in the form of a multi-dimensional array.</p>
|
| 201 |
|
| 202 | <p>Here's a simple example. Add this to your controller:</p>
|
| 203 |
|
| 204 | <textarea class="textarea" style="width:100%" cols="50" rows="17">
|
Derek Allard | 78c3fa8 | 2008-01-21 15:11:13 +0000 | [diff] [blame] | 205 | <?php
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 206 | class Blog extends Controller {
|
| 207 |
|
| 208 | function index()
|
| 209 | {
|
| 210 | $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
|
| 211 |
|
| 212 | $data['title'] = "My Real Title";
|
| 213 | $data['heading'] = "My Real Heading";
|
| 214 |
|
| 215 | $this->load->view('blogview', $data);
|
| 216 | }
|
| 217 | }
|
Derek Allard | 78c3fa8 | 2008-01-21 15:11:13 +0000 | [diff] [blame] | 218 | ?>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 219 | </textarea>
|
| 220 |
|
| 221 |
|
| 222 | <p>Now open your view file and create a loop:</p>
|
| 223 |
|
| 224 |
|
| 225 | <textarea class="textarea" style="width:100%" cols="50" rows="24">
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 226 | <html>
|
| 227 | <head>
|
Derek Allard | 2005ffe | 2007-09-15 20:11:54 +0000 | [diff] [blame] | 228 | <title><?php echo $title;?></title>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 229 | </head>
|
| 230 | <body>
|
Derek Allard | 2005ffe | 2007-09-15 20:11:54 +0000 | [diff] [blame] | 231 | <h1><?php echo $heading;?></h1>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 232 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 233 | <h3>My Todo List</h3>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 234 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 235 | <ul>
|
| 236 | <?php foreach($todo_list as $item):?>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 237 |
|
Derek Allard | 2005ffe | 2007-09-15 20:11:54 +0000 | [diff] [blame] | 238 | <li><?php echo $item;?></li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 239 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 240 | <?php endforeach;?>
|
| 241 | </ul>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 242 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 243 | </body>
|
| 244 | </html>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 245 | </textarea>
|
Derek Allard | 78c3fa8 | 2008-01-21 15:11:13 +0000 | [diff] [blame] | 246 | <p><strong>Note:</strong> You'll notice that in the example above we are using PHP's alternative syntax. If you
|
| 247 | are not familiar with it you can read about it <a href="alternative_php.html">here</a>.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 248 |
|
| 249 |
|
| 250 | </div>
|
| 251 | <!-- END CONTENT -->
|
| 252 |
|
| 253 |
|
| 254 | <div id="footer">
|
| 255 | <p>
|
Rick Ellis | f5c7a6f | 2008-09-10 22:12:51 +0000 | [diff] [blame] | 256 | Previous Topic: <a href="reserved_names.html">Reserved Names</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 257 | ·
|
| 258 | <a href="#top">Top of Page</a> ·
|
| 259 | <a href="../index.html">User Guide Home</a> ·
|
| 260 | Next Topic: <a href="models.html">Models</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 261 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 262 | <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] | 263 | </div>
|
| 264 |
|
| 265 | </body>
|
| 266 | </html> |