blob: 7d0accafdf155e8f85bda79dcbcb85247c9830ed [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#####
2Views
3#####
4
5A view is simply a web page, or a page fragment, like a header, footer,
6sidebar, etc. In fact, views can flexibly be embedded within other views
7(within other views, etc., etc.) if you need this type of hierarchy.
8
9Views are never called directly, they must be loaded by a
10:doc:`controller <controllers>`. Remember that in an MVC framework, the
11Controller acts as the traffic cop, so it is responsible for fetching a
12particular view. If you have not read the
13:doc:`Controllers <controllers>` page you should do so before
14continuing.
15
16Using the example controller you created in the
17:doc:`controller <controllers>` page, let's add a view to it.
18
19Creating a View
20===============
21
22Using your text editor, create a file called blogview.php, and put this
Derek Jones9607e732011-10-05 16:42:42 -050023in it::
Derek Jones8ede1a22011-10-05 13:34:52 -050024
Derek Jones9607e732011-10-05 16:42:42 -050025 <html>
26 <head>
27 <title>My Blog</title>
28 </head>
29 <body>
30 <h1>Welcome to my Blog!</h1>
31 </body>
32 </html>
33
Derek Jones8ede1a22011-10-05 13:34:52 -050034Then save the file in your application/views/ folder.
35
36Loading a View
37==============
38
39To load a particular view file you will use the following function::
40
41 $this->load->view('name');
42
43Where name is the name of your view file. Note: The .php file extension
44does not need to be specified unless you use something other than .php.
45
46Now, open the controller file you made earlier called blog.php, and
Derek Jones9607e732011-10-05 16:42:42 -050047replace the echo statement with the view loading function::
Derek Jones8ede1a22011-10-05 13:34:52 -050048
Derek Jones9607e732011-10-05 16:42:42 -050049 <?php
50 class Blog extends CI_Controller {
51
52 function index()
53 {
54 $this->load->view('blogview');
55 }
56 }
57 ?>
58
Derek Jones8ede1a22011-10-05 13:34:52 -050059If you visit your site using the URL you did earlier you should see your
60new view. The URL was similar to this::
61
62 example.com/index.php/blog/
63
64Loading multiple views
65======================
66
67CodeIgniter will intelligently handle multiple calls to
68$this->load->view from within a controller. If more than one call
69happens they will be appended together. For example, you may wish to
70have a header view, a menu view, a content view, and a footer view. That
71might look something like this::
72
Derek Jones9607e732011-10-05 16:42:42 -050073 <?php
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Derek Jones9607e732011-10-05 16:42:42 -050075 class Page extends CI_Controller {
76
77 function index()
78 {
79 $data['page_title'] = 'Your title';
80 $this->load->view('header');
81 $this->load->view('menu');
82 $this->load->view('content', $data);
83 $this->load->view('footer');
84 }
85
86 }
87 ?>
Derek Jones8ede1a22011-10-05 13:34:52 -050088
89In the example above, we are using "dynamically added data", which you
90will see below.
91
92Storing Views within Sub-folders
93================================
94
95Your view files can also be stored within sub-folders if you prefer that
96type of organization. When doing so you will need to include the folder
97name loading the view. Example::
98
99 $this->load->view('folder_name/file_name');
100
101Adding Dynamic Data to the View
102===============================
103
104Data is passed from the controller to the view by way of an **array** or
105an **object** in the second parameter of the view loading function. Here
106is an example using an array::
107
Derek Jones9607e732011-10-05 16:42:42 -0500108 $data = array(
109 'title' => 'My Title',
110 'heading' => 'My Heading',
111 'message' => 'My Message'
112 );
113
114 $this->load->view('blogview', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
116And here's an example using an object::
117
Derek Jones9607e732011-10-05 16:42:42 -0500118 $data = new Someclass();
119 $this->load->view('blogview', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
121Note: If you use an object, the class variables will be turned into
122array elements.
123
Derek Jones9607e732011-10-05 16:42:42 -0500124Let's try it with your controller file. Open it add this code::
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
Derek Jones9607e732011-10-05 16:42:42 -0500126 <?php
127 class Blog extends CI_Controller {
128
129 function index()
130 {
131 $data['title'] = "My Real Title";
132 $data['heading'] = "My Real Heading";
133
134 $this->load->view('blogview', $data);
135 }
136 }
137 ?>
138
Derek Jones8ede1a22011-10-05 13:34:52 -0500139Now open your view file and change the text to variables that correspond
Derek Jones9607e732011-10-05 16:42:42 -0500140to the array keys in your data::
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Derek Jones9607e732011-10-05 16:42:42 -0500142 <html>
143 <head>
144 <title><?php echo $title;?></title>
145 </head>
146 <body>
147 <h1><?php echo $heading;?></h1>
148 </body>
149 </html>
150
Derek Jones8ede1a22011-10-05 13:34:52 -0500151Then load the page at the URL you've been using and you should see the
152variables replaced.
153
154Creating Loops
155==============
156
157The data array you pass to your view files is not limited to simple
158variables. You can pass multi dimensional arrays, which can be looped to
159generate multiple rows. For example, if you pull data from your database
160it will typically be in the form of a multi-dimensional array.
161
Derek Jones9607e732011-10-05 16:42:42 -0500162Here's a simple example. Add this to your controller::
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Derek Jones9607e732011-10-05 16:42:42 -0500164 <?php
165 class Blog extends CI_Controller {
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Derek Jones9607e732011-10-05 16:42:42 -0500167 function index()
168 {
169 $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
170
171 $data['title'] = "My Real Title";
172 $data['heading'] = "My Real Heading";
173
174 $this->load->view('blogview', $data);
175 }
176 }
177 ?>
178
179Now open your view file and create a loop::
180
181 <html>
182 <head>
183 <title><?php echo $title;?></title>
184 </head>
185 <body>
186 <h1><?php echo $heading;?></h1>
187
188 <h3>My Todo List</h3>
189
190 <ul>
191 <?php foreach ($todo_list as $item):?>
192
193 <li><?php echo $item;?></li>
194
195 <?php endforeach;?>
196 </ul>
197
198 </body>
199 </html>
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
201.. note:: You'll notice that in the example above we are using PHP's
202 alternative syntax. If you are not familiar with it you can read about
203 it `here </general/alternative_php>`.
204
205Returning views as data
206=======================
207
208There is a third **optional** parameter lets you change the behavior of
209the function so that it returns data as a string rather than sending it
210to your browser. This can be useful if you want to process the data in
211some way. If you set the parameter to true (boolean) it will return
212data. The default behavior is false, which sends it to your browser.
213Remember to assign it to a variable if you want the data returned::
214
215 $string = $this->load->view('myfile', '', true);
216