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 |
|
Derek Allard | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Views</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 Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.0</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 Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 43 | <a href="http://www.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 |
|
| 114 | <code>www.your-site.com/index.php/<var>blog</var>/</code>
|
| 115 |
|
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 116 | <h2>Storing Views within Sub-folders</h2>
|
| 117 |
|
| 118 | <p>Your view files can also be stored within sub-folders if you prefer that type of organization. When doing so you will need
|
| 119 | to include the folder name loading the view. Example:</p>
|
| 120 |
|
| 121 | <code>$this->load->view('<kbd>folder_name</kbd>/<var>file_name</var>');</code>
|
| 122 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 123 |
|
| 124 | <h2>Adding Dynamic Data to the View</h2>
|
| 125 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 126 | <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] | 127 | parameter of the view loading function. Here is an example using an array:</p>
|
| 128 |
|
| 129 | <code>$data = array(<br />
|
| 130 | 'title' => 'My Title',<br />
|
Rick Ellis | 0cb3404 | 2007-03-23 21:02:11 +0000 | [diff] [blame] | 131 | 'heading' => 'My Heading',<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 132 | 'message' => 'My Message'<br />
|
| 133 | );<br />
|
| 134 | <br />
|
| 135 | $this->load->view('blogview', <var>$data</var>);</code>
|
| 136 |
|
| 137 | <p>And here's an example using an object:</p>
|
| 138 |
|
| 139 | <code>$data = new Someclass();<br />
|
| 140 | $this->load->view('blogview', <var>$data</var>);</code>
|
| 141 |
|
| 142 | <p>Note: If you use an object, the class variables will be turned into array elements.</p>
|
| 143 |
|
| 144 |
|
| 145 | <p>Let's try it with your controller file. Open it add this code:</p>
|
| 146 |
|
| 147 | <textarea class="textarea" style="width:100%" cols="50" rows="14">
|
| 148 | <?php
|
| 149 | class Blog extends Controller {
|
| 150 |
|
| 151 | function index()
|
| 152 | {
|
| 153 | $data['title'] = "My Real Title";
|
| 154 | $data['heading'] = "My Real Heading";
|
| 155 |
|
| 156 | $this->load->view('blogview', $data);
|
| 157 | }
|
| 158 | }
|
| 159 | ?>
|
| 160 | </textarea>
|
| 161 |
|
| 162 |
|
| 163 | <p>Now open your view file and change the text to variables that correspond to the array keys in your data:</p>
|
| 164 |
|
| 165 |
|
| 166 | <textarea class="textarea" style="width:100%" cols="50" rows="10">
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 167 | <html>
|
| 168 | <head>
|
| 169 | <title><?php echo $title;?></title>
|
| 170 | </head>
|
| 171 | <body>
|
| 172 | <h1><?php echo $heading;?></h1>
|
| 173 | </body>
|
| 174 | </html>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 175 | </textarea>
|
| 176 |
|
| 177 | <p>Then load the page at the URL you've been using and you should see the variables replaced.</p>
|
| 178 |
|
admin | e7e1dcd | 2006-10-21 18:04:01 +0000 | [diff] [blame] | 179 | <p><strong>Note:</strong> You'll notice that in the example above we are using PHP's alternative syntax. If you
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 180 | are not familiar with it you can read about it <a href="alternative_php.html">here</a>.</p>
|
| 181 |
|
| 182 | <h2>Creating Loops</h2>
|
| 183 |
|
| 184 | <p>The data array you pass to your view files is not limited to simple variables. You can
|
| 185 | pass multi dimensional arrays, which can be looped to generate multiple rows. For example, if you
|
| 186 | pull data from your database it will typically be in the form of a multi-dimensional array.</p>
|
| 187 |
|
| 188 | <p>Here's a simple example. Add this to your controller:</p>
|
| 189 |
|
| 190 | <textarea class="textarea" style="width:100%" cols="50" rows="17">
|
| 191 | <?php
|
| 192 | class Blog extends Controller {
|
| 193 |
|
| 194 | function index()
|
| 195 | {
|
| 196 | $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
|
| 197 |
|
| 198 | $data['title'] = "My Real Title";
|
| 199 | $data['heading'] = "My Real Heading";
|
| 200 |
|
| 201 | $this->load->view('blogview', $data);
|
| 202 | }
|
| 203 | }
|
| 204 | ?>
|
| 205 | </textarea>
|
| 206 |
|
| 207 |
|
| 208 | <p>Now open your view file and create a loop:</p>
|
| 209 |
|
| 210 |
|
| 211 | <textarea class="textarea" style="width:100%" cols="50" rows="24">
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 212 | <html>
|
| 213 | <head>
|
Derek Allard | 2005ffe | 2007-09-15 20:11:54 +0000 | [diff] [blame] | 214 | <title><?php echo $title;?></title>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 215 | </head>
|
| 216 | <body>
|
Derek Allard | 2005ffe | 2007-09-15 20:11:54 +0000 | [diff] [blame] | 217 | <h1><?php echo $heading;?></h1>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 218 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 219 | <h3>My Todo List</h3>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 220 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 221 | <ul>
|
| 222 | <?php foreach($todo_list as $item):?>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 223 |
|
Derek Allard | 2005ffe | 2007-09-15 20:11:54 +0000 | [diff] [blame] | 224 | <li><?php echo $item;?></li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 225 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 226 | <?php endforeach;?>
|
| 227 | </ul>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 228 |
|
| 229 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 230 | </body>
|
| 231 | </html>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 232 | </textarea>
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 | </div>
|
| 237 | <!-- END CONTENT -->
|
| 238 |
|
| 239 |
|
| 240 | <div id="footer">
|
| 241 | <p>
|
| 242 | Previous Topic: <a href="controllers.html">Controllers</a>
|
| 243 | ·
|
| 244 | <a href="#top">Top of Page</a> ·
|
| 245 | <a href="../index.html">User Guide Home</a> ·
|
| 246 | Next Topic: <a href="models.html">Models</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 247 | </p>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 248 | <p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 249 | </div>
|
| 250 |
|
| 251 | </body>
|
| 252 | </html> |