blob: cb60ce20f1abcd8af4f5685aefefc1a15590302e [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
23in it:
24
25<html> <head> <title>My Blog</title> </head> <body> <h1>Welcome to my
26Blog!</h1> </body> </html>
27Then save the file in your application/views/ folder.
28
29Loading a View
30==============
31
32To load a particular view file you will use the following function::
33
34 $this->load->view('name');
35
36Where name is the name of your view file. Note: The .php file extension
37does not need to be specified unless you use something other than .php.
38
39Now, open the controller file you made earlier called blog.php, and
40replace the echo statement with the view loading function:
41
42<?php class Blog extends CI_Controller { function index() {
43$this->load->view('blogview'); } } ?>
44If you visit your site using the URL you did earlier you should see your
45new view. The URL was similar to this::
46
47 example.com/index.php/blog/
48
49Loading multiple views
50======================
51
52CodeIgniter will intelligently handle multiple calls to
53$this->load->view from within a controller. If more than one call
54happens they will be appended together. For example, you may wish to
55have a header view, a menu view, a content view, and a footer view. That
56might look something like this::
57
58 <?php class Page extends CI_Controller {    function index()    {       $data['page_title'] = 'Your title';       $this->load->view('header');       $this->load->view('menu');       $this->load->view('content', $data);       $this->load->view('footer');    } } ?>
59
60
61In the example above, we are using "dynamically added data", which you
62will see below.
63
64Storing Views within Sub-folders
65================================
66
67Your view files can also be stored within sub-folders if you prefer that
68type of organization. When doing so you will need to include the folder
69name loading the view. Example::
70
71 $this->load->view('folder_name/file_name');
72
73Adding Dynamic Data to the View
74===============================
75
76Data is passed from the controller to the view by way of an **array** or
77an **object** in the second parameter of the view loading function. Here
78is an example using an array::
79
80 $data = array(                'title' => 'My Title',                'heading' => 'My Heading',                'message' => 'My Message'           ); $this->load->view('blogview', $data);
81
82And here's an example using an object::
83
84 $data = new Someclass(); $this->load->view('blogview', $data);
85
86Note: If you use an object, the class variables will be turned into
87array elements.
88
89Let's try it with your controller file. Open it add this code:
90
91<?php class Blog extends CI_Controller { function index() {
92$data['title'] = "My Real Title"; $data['heading'] = "My Real Heading";
93$this->load->view('blogview', $data); } } ?>
94Now open your view file and change the text to variables that correspond
95to the array keys in your data:
96
97<html> <head> <title><?php echo $title;?></title> </head> <body>
98<h1><?php echo $heading;?></h1> </body> </html>
99Then load the page at the URL you've been using and you should see the
100variables replaced.
101
102Creating Loops
103==============
104
105The data array you pass to your view files is not limited to simple
106variables. You can pass multi dimensional arrays, which can be looped to
107generate multiple rows. For example, if you pull data from your database
108it will typically be in the form of a multi-dimensional array.
109
110Here's a simple example. Add this to your controller:
111
112<?php class Blog extends CI_Controller { function index() {
113$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
114$data['title'] = "My Real Title"; $data['heading'] = "My Real Heading";
115$this->load->view('blogview', $data); } } ?>
116Now open your view file and create a loop:
117
118<html> <head> <title><?php echo $title;?></title> </head> <body>
119<h1><?php echo $heading;?></h1> <h3>My Todo List</h3> <ul> <?php foreach
120($todo_list as $item):?> <li><?php echo $item;?></li> <?php
121endforeach;?> </ul> </body> </html>
122
123.. note:: You'll notice that in the example above we are using PHP's
124 alternative syntax. If you are not familiar with it you can read about
125 it `here </general/alternative_php>`.
126
127Returning views as data
128=======================
129
130There is a third **optional** parameter lets you change the behavior of
131the function so that it returns data as a string rather than sending it
132to your browser. This can be useful if you want to process the data in
133some way. If you set the parameter to true (boolean) it will return
134data. The default behavior is false, which sends it to your browser.
135Remember to assign it to a variable if you want the data returned::
136
137 $string = $this->load->view('myfile', '', true);
138