blob: 2fc0cb2ca4bc49cf05e8b2dfc8f5169e3088a973 [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>
purwandi5ebf9d12011-10-07 16:09:13 +070027 <title>My Blog</title>
Derek Jones9607e732011-10-05 16:42:42 -050028 </head>
29 <body>
30 <h1>Welcome to my Blog!</h1>
31 </body>
32 </html>
33
Andrey Andreev16a704c2012-11-09 17:25:00 +020034Then save the file in your *application/views/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050035
36Loading a View
37==============
38
Andrey Andreev16a704c2012-11-09 17:25:00 +020039To load a particular view file you will use the following method::
Derek Jones8ede1a22011-10-05 13:34:52 -050040
41 $this->load->view('name');
42
Andrey Andreev16a704c2012-11-09 17:25:00 +020043Where name is the name of your view file.
44
45.. note:: The .php file extension does not need to be specified
46 unless you use something other than .php.
Derek Jones8ede1a22011-10-05 13:34:52 -050047
Andrey Andreev20292312013-07-22 14:29:10 +030048Now, open the controller file you made earlier called Blog.php, and
Andrey Andreev16a704c2012-11-09 17:25:00 +020049replace the echo statement with the view loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -050050
Derek Jones9607e732011-10-05 16:42:42 -050051 <?php
52 class Blog extends CI_Controller {
53
Andrey Andreevd8e1ac72012-03-26 22:22:37 +030054 public function index()
Derek Jones9607e732011-10-05 16:42:42 -050055 {
56 $this->load->view('blogview');
57 }
58 }
Derek Jones9607e732011-10-05 16:42:42 -050059
Derek Jones8ede1a22011-10-05 13:34:52 -050060If you visit your site using the URL you did earlier you should see your
61new view. The URL was similar to this::
62
63 example.com/index.php/blog/
64
65Loading multiple views
66======================
67
68CodeIgniter will intelligently handle multiple calls to
Andrey Andreev16a704c2012-11-09 17:25:00 +020069``$this->load->view()`` from within a controller. If more than one call
Derek Jones8ede1a22011-10-05 13:34:52 -050070happens they will be appended together. For example, you may wish to
71have a header view, a menu view, a content view, and a footer view. That
72might look something like this::
73
Derek Jones9607e732011-10-05 16:42:42 -050074 <?php
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Derek Jones9607e732011-10-05 16:42:42 -050076 class Page extends CI_Controller {
77
Andrey Andreevd8e1ac72012-03-26 22:22:37 +030078 public function index()
79 {
80 $data['page_title'] = 'Your title';
81 $this->load->view('header');
82 $this->load->view('menu');
83 $this->load->view('content', $data);
84 $this->load->view('footer');
85 }
Derek Jones9607e732011-10-05 16:42:42 -050086
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
Andrey Andreev16a704c2012-11-09 17:25:00 +020092Storing Views within Sub-directories
93====================================
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Andrey Andreev16a704c2012-11-09 17:25:00 +020095Your view files can also be stored within sub-directories if you prefer
96that type of organization. When doing so you will need to include the
97directory name loading the view. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050098
Andrey Andreev16a704c2012-11-09 17:25:00 +020099 $this->load->view('directory_name/file_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
101Adding Dynamic Data to the View
102===============================
103
104Data is passed from the controller to the view by way of an **array** or
Andrey Andreev16a704c2012-11-09 17:25:00 +0200105an **object** in the second parameter of the view loading method. Here
Derek Jones8ede1a22011-10-05 13:34:52 -0500106is an example using an array::
107
Derek Jones9607e732011-10-05 16:42:42 -0500108 $data = array(
Andrey Andreev16a704c2012-11-09 17:25:00 +0200109 'title' => 'My Title',
110 'heading' => 'My Heading',
111 'message' => 'My Message'
112 );
Derek Jones9607e732011-10-05 16:42:42 -0500113
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
Andrey Andreev16a704c2012-11-09 17:25:00 +0200121.. note:: If you use an object, the class variables will be turned
122 into array elements.
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
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
Andrey Andreevd8e1ac72012-03-26 22:22:37 +0300129 public function index()
Derek Jones9607e732011-10-05 16:42:42 -0500130 {
131 $data['title'] = "My Real Title";
132 $data['heading'] = "My Real Heading";
133
134 $this->load->view('blogview', $data);
135 }
136 }
Derek Jones9607e732011-10-05 16:42:42 -0500137
Derek Jones8ede1a22011-10-05 13:34:52 -0500138Now open your view file and change the text to variables that correspond
Derek Jones9607e732011-10-05 16:42:42 -0500139to the array keys in your data::
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Derek Jones9607e732011-10-05 16:42:42 -0500141 <html>
142 <head>
purwandi5ebf9d12011-10-07 16:09:13 +0700143 <title><?php echo $title;?></title>
Derek Jones9607e732011-10-05 16:42:42 -0500144 </head>
145 <body>
146 <h1><?php echo $heading;?></h1>
147 </body>
148 </html>
149
Derek Jones8ede1a22011-10-05 13:34:52 -0500150Then load the page at the URL you've been using and you should see the
151variables replaced.
152
153Creating Loops
154==============
155
156The data array you pass to your view files is not limited to simple
157variables. You can pass multi dimensional arrays, which can be looped to
158generate multiple rows. For example, if you pull data from your database
159it will typically be in the form of a multi-dimensional array.
160
Derek Jones9607e732011-10-05 16:42:42 -0500161Here's a simple example. Add this to your controller::
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
Derek Jones9607e732011-10-05 16:42:42 -0500163 <?php
164 class Blog extends CI_Controller {
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Andrey Andreevd8e1ac72012-03-26 22:22:37 +0300166 public function index()
Derek Jones9607e732011-10-05 16:42:42 -0500167 {
168 $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
169
170 $data['title'] = "My Real Title";
171 $data['heading'] = "My Real Heading";
172
173 $this->load->view('blogview', $data);
174 }
175 }
Derek Jones9607e732011-10-05 16:42:42 -0500176
177Now open your view file and create a loop::
178
179 <html>
180 <head>
purwandi5ebf9d12011-10-07 16:09:13 +0700181 <title><?php echo $title;?></title>
Derek Jones9607e732011-10-05 16:42:42 -0500182 </head>
183 <body>
purwandi5ebf9d12011-10-07 16:09:13 +0700184 <h1><?php echo $heading;?></h1>
185
186 <h3>My Todo List</h3>
Derek Jones9607e732011-10-05 16:42:42 -0500187
purwandi5ebf9d12011-10-07 16:09:13 +0700188 <ul>
189 <?php foreach ($todo_list as $item):?>
190
191 <li><?php echo $item;?></li>
192
193 <?php endforeach;?>
194 </ul>
Derek Jones9607e732011-10-05 16:42:42 -0500195
196 </body>
197 </html>
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
199.. note:: You'll notice that in the example above we are using PHP's
200 alternative syntax. If you are not familiar with it you can read about
Andrey Andreev16a704c2012-11-09 17:25:00 +0200201 it :doc:`here <alternative_php>`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
203Returning views as data
204=======================
205
206There is a third **optional** parameter lets you change the behavior of
Andrey Andreev16a704c2012-11-09 17:25:00 +0200207the method so that it returns data as a string rather than sending it
Derek Jones8ede1a22011-10-05 13:34:52 -0500208to your browser. This can be useful if you want to process the data in
Andrey Andreev16a704c2012-11-09 17:25:00 +0200209some way. If you set the parameter to TRUE (boolean) it will return
Derek Jones8ede1a22011-10-05 13:34:52 -0500210data. The default behavior is false, which sends it to your browser.
211Remember to assign it to a variable if you want the data returned::
212
Andrey Andreev16a704c2012-11-09 17:25:00 +0200213 $string = $this->load->view('myfile', '', TRUE);