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 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | Then save the file in your application/views/ folder. |
| 35 | |
| 36 | Loading a View |
| 37 | ============== |
| 38 | |
| 39 | To load a particular view file you will use the following function:: |
| 40 | |
| 41 | $this->load->view('name'); |
| 42 | |
| 43 | Where name is the name of your view file. Note: The .php file extension |
| 44 | does not need to be specified unless you use something other than .php. |
| 45 | |
| 46 | Now, open the controller file you made earlier called blog.php, and |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 47 | replace the echo statement with the view loading function:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 49 | <?php |
| 50 | class Blog extends CI_Controller { |
| 51 | |
| 52 | function index() |
| 53 | { |
| 54 | $this->load->view('blogview'); |
| 55 | } |
| 56 | } |
| 57 | ?> |
| 58 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | If you visit your site using the URL you did earlier you should see your |
| 60 | new view. The URL was similar to this:: |
| 61 | |
| 62 | example.com/index.php/blog/ |
| 63 | |
| 64 | Loading multiple views |
| 65 | ====================== |
| 66 | |
| 67 | CodeIgniter will intelligently handle multiple calls to |
| 68 | $this->load->view from within a controller. If more than one call |
| 69 | happens they will be appended together. For example, you may wish to |
| 70 | have a header view, a menu view, a content view, and a footer view. That |
| 71 | might look something like this:: |
| 72 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 73 | <?php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 75 | 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 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 | |
| 92 | Storing Views within Sub-folders |
| 93 | ================================ |
| 94 | |
| 95 | Your view files can also be stored within sub-folders if you prefer that |
| 96 | type of organization. When doing so you will need to include the folder |
| 97 | name loading the view. Example:: |
| 98 | |
| 99 | $this->load->view('folder_name/file_name'); |
| 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 |
| 105 | an **object** in the second parameter of the view loading function. Here |
| 106 | is an example using an array:: |
| 107 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 108 | $data = array( |
| 109 | 'title' => 'My Title', |
| 110 | 'heading' => 'My Heading', |
| 111 | 'message' => 'My Message' |
| 112 | ); |
| 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 | |
| 121 | Note: If you use an object, the class variables will be turned into |
| 122 | array elements. |
| 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 | |
| 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | 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] | 140 | to the array keys in your data:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 142 | <html> |
| 143 | <head> |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 144 | <title><?php echo $title;?></title> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 145 | </head> |
| 146 | <body> |
| 147 | <h1><?php echo $heading;?></h1> |
| 148 | </body> |
| 149 | </html> |
| 150 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 151 | Then load the page at the URL you've been using and you should see the |
| 152 | variables replaced. |
| 153 | |
| 154 | Creating Loops |
| 155 | ============== |
| 156 | |
| 157 | The data array you pass to your view files is not limited to simple |
| 158 | variables. You can pass multi dimensional arrays, which can be looped to |
| 159 | generate multiple rows. For example, if you pull data from your database |
| 160 | it will typically be in the form of a multi-dimensional array. |
| 161 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 162 | Here's a simple example. Add this to your controller:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 164 | <?php |
| 165 | class Blog extends CI_Controller { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 166 | |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 167 | 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 | |
| 179 | Now open your view file and create a loop:: |
| 180 | |
| 181 | <html> |
| 182 | <head> |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 183 | <title><?php echo $title;?></title> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 184 | </head> |
| 185 | <body> |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 186 | <h1><?php echo $heading;?></h1> |
| 187 | |
| 188 | <h3>My Todo List</h3> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 189 | |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 190 | <ul> |
| 191 | <?php foreach ($todo_list as $item):?> |
| 192 | |
| 193 | <li><?php echo $item;?></li> |
| 194 | |
| 195 | <?php endforeach;?> |
| 196 | </ul> |
Derek Jones | 9607e73 | 2011-10-05 16:42:42 -0500 | [diff] [blame] | 197 | |
| 198 | </body> |
| 199 | </html> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 200 | |
| 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 |
purwandi | 5ebf9d1 | 2011-10-07 16:09:13 +0700 | [diff] [blame] | 203 | it :doc:`here </general/alternative_php>`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 204 | |
| 205 | Returning views as data |
| 206 | ======================= |
| 207 | |
| 208 | There is a third **optional** parameter lets you change the behavior of |
| 209 | the function so that it returns data as a string rather than sending it |
| 210 | to your browser. This can be useful if you want to process the data in |
| 211 | some way. If you set the parameter to true (boolean) it will return |
| 212 | data. The default behavior is false, which sends it to your browser. |
| 213 | Remember to assign it to a variable if you want the data returned:: |
| 214 | |
| 215 | $string = $this->load->view('myfile', '', true); |
| 216 | |