Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ##### |
| 2 | Views |
| 3 | ##### |
| 4 | |
| 5 | A view is simply a web page, or a page fragment, like a header, footer, |
| 6 | sidebar, 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 | |
| 9 | Views are never called directly, they must be loaded by a |
| 10 | :doc:`controller <controllers>`. Remember that in an MVC framework, the |
| 11 | Controller acts as the traffic cop, so it is responsible for fetching a |
| 12 | particular view. If you have not read the |
| 13 | :doc:`Controllers <controllers>` page you should do so before |
| 14 | continuing. |
| 15 | |
| 16 | Using the example controller you created in the |
| 17 | :doc:`controller <controllers>` page, let's add a view to it. |
| 18 | |
| 19 | Creating a View |
| 20 | =============== |
| 21 | |
| 22 | Using your text editor, create a file called blogview.php, and put this |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 23 | in it:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 25 | <html> |
| 26 | <head> |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 27 | <title>My Blog</title> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 28 | </head> |
| 29 | <body> |
| 30 | <h1>Welcome to my Blog!</h1> |
| 31 | </body> |
| 32 | </html> |
| 33 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 34 | Then save the file in your *application/views/* directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
| 36 | Loading a View |
| 37 | ============== |
| 38 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 39 | To load a particular view file you will use the following method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
| 41 | $this->load->view('name'); |
| 42 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 43 | Where 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 48 | Now, open the controller file you made earlier called Blog.php, and |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 49 | replace the echo statement with the view loading method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 51 | <?php |
| 52 | class Blog extends CI_Controller { |
| 53 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 54 | public function index() |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 55 | { |
| 56 | $this->load->view('blogview'); |
| 57 | } |
| 58 | } |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 59 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | If you visit your site using the URL you did earlier you should see your |
| 61 | new view. The URL was similar to this:: |
| 62 | |
| 63 | example.com/index.php/blog/ |
| 64 | |
| 65 | Loading multiple views |
| 66 | ====================== |
| 67 | |
| 68 | CodeIgniter will intelligently handle multiple calls to |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 69 | ``$this->load->view()`` from within a controller. If more than one call |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | happens they will be appended together. For example, you may wish to |
| 71 | have a header view, a menu view, a content view, and a footer view. That |
| 72 | might look something like this:: |
| 73 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 74 | <?php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 76 | class Page extends CI_Controller { |
| 77 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 78 | 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 Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 86 | |
| 87 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
| 89 | In the example above, we are using "dynamically added data", which you |
| 90 | will see below. |
| 91 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 92 | Storing Views within Sub-directories |
| 93 | ==================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 95 | Your view files can also be stored within sub-directories if you prefer |
| 96 | that type of organization. When doing so you will need to include the |
| 97 | directory name loading the view. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 99 | $this->load->view('directory_name/file_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
| 101 | Adding Dynamic Data to the View |
| 102 | =============================== |
| 103 | |
| 104 | Data is passed from the controller to the view by way of an **array** or |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 105 | an **object** in the second parameter of the view loading method. Here |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 106 | is an example using an array:: |
| 107 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 108 | $data = array( |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 109 | 'title' => 'My Title', |
| 110 | 'heading' => 'My Heading', |
| 111 | 'message' => 'My Message' |
| 112 | ); |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 113 | |
| 114 | $this->load->view('blogview', $data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 115 | |
| 116 | And here's an example using an object:: |
| 117 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 118 | $data = new Someclass(); |
| 119 | $this->load->view('blogview', $data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 121 | .. note:: If you use an object, the class variables will be turned |
| 122 | into array elements. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 124 | Let's try it with your controller file. Open it add this code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 126 | <?php |
| 127 | class Blog extends CI_Controller { |
| 128 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 129 | public function index() |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 130 | { |
| 131 | $data['title'] = "My Real Title"; |
| 132 | $data['heading'] = "My Real Heading"; |
| 133 | |
| 134 | $this->load->view('blogview', $data); |
| 135 | } |
| 136 | } |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 137 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | Now open your view file and change the text to variables that correspond |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 139 | to the array keys in your data:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 140 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 141 | <html> |
| 142 | <head> |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 143 | <title><?php echo $title;?></title> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 144 | </head> |
| 145 | <body> |
| 146 | <h1><?php echo $heading;?></h1> |
| 147 | </body> |
| 148 | </html> |
| 149 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | Then load the page at the URL you've been using and you should see the |
| 151 | variables replaced. |
| 152 | |
| 153 | Creating Loops |
| 154 | ============== |
| 155 | |
| 156 | The data array you pass to your view files is not limited to simple |
| 157 | variables. You can pass multi dimensional arrays, which can be looped to |
| 158 | generate multiple rows. For example, if you pull data from your database |
| 159 | it will typically be in the form of a multi-dimensional array. |
| 160 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 161 | Here's a simple example. Add this to your controller:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 163 | <?php |
| 164 | class Blog extends CI_Controller { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 165 | |
Andrey Andreev | d8e1ac7 | 2012-03-26 22:22:37 +0300 | [diff] [blame] | 166 | public function index() |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 167 | { |
| 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 Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 176 | |
| 177 | Now open your view file and create a loop:: |
| 178 | |
| 179 | <html> |
| 180 | <head> |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 181 | <title><?php echo $title;?></title> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 182 | </head> |
| 183 | <body> |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 184 | <h1><?php echo $heading;?></h1> |
| 185 | |
| 186 | <h3>My Todo List</h3> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 187 | |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 188 | <ul> |
| 189 | <?php foreach ($todo_list as $item):?> |
| 190 | |
| 191 | <li><?php echo $item;?></li> |
| 192 | |
| 193 | <?php endforeach;?> |
| 194 | </ul> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 195 | |
| 196 | </body> |
| 197 | </html> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | |
| 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 Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 201 | it :doc:`here <alternative_php>`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
| 203 | Returning views as data |
| 204 | ======================= |
| 205 | |
| 206 | There is a third **optional** parameter lets you change the behavior of |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 207 | the method so that it returns data as a string rather than sending it |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | to your browser. This can be useful if you want to process the data in |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 209 | some way. If you set the parameter to TRUE (boolean) it will return |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 210 | data. The default behavior is false, which sends it to your browser. |
| 211 | Remember to assign it to a variable if you want the data returned:: |
| 212 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 213 | $string = $this->load->view('myfile', '', TRUE); |