From 37f4b9caa02783e06dd7c5318200113409a0deb1 Mon Sep 17 00:00:00 2001
From: Derek Jones
Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller acts as the +
Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have not read the Controllers page you should do so before continuing.
@@ -90,7 +90,7 @@ you should do so before continuing.$this->load->view('name');
-Where name is the name of your view file. Note: The .php file extension does not need to be specified unless you use something other than .php.
+Where name is the name of your view file. Note: The .php file extension does not need to be specified unless you use something other than .php.
Now, open the controller file you made earlier called blog.php, and replace the echo statement with the view loading function:
@@ -109,19 +109,19 @@ class Blog extends CI_Controller { -If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this:
+If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this:
example.com/index.php/blog/
CodeIgniter will intelligently handle multiple calls to $this->load->view from within a controller. If more than one call happens they will be appended together. For example, you may wish to have a header view, a menu view, a content view, and a footer view. That might look something like this:
+CodeIgniter will intelligently handle multiple calls to $this->load->view from within a controller. If more than one call happens they will be appended together. For example, you may wish to have a header view, a menu view, a content view, and a footer view. That might look something like this:
<?php
class Page extends CI_Controller {
function index()
{
- $data['page_title'] = 'Your title';
+ $data['page_title'] = 'Your title';
$this->load->view('header');
$this->load->view('menu');
$this->load->view('content', $data);
@@ -132,8 +132,8 @@ class Page extends CI_Controller {
?>
In the example above, we are using "dynamically added data", which you will see below.
Your view files can also be stored within sub-folders if you prefer that type of organization. When doing so you will need -to include the folder name loading the view. Example:
+Your view files can also be stored within sub-folders if you prefer that type of organization. When doing so you will need +to include the folder name loading the view. Example:
$this->load->view('folder_name/file_name');
@@ -159,7 +159,7 @@ $this->load->view('blogview', $data);
Note: If you use an object, the class variables will be turned into array elements.
-Let's try it with your controller file. Open it add this code:
+Let's try it with your controller file. Open it add this code:
-Note: You'll notice that in the example above we are using PHP's alternative syntax. If you +
Note: You'll notice that in the example above we are using PHP's alternative syntax. If you are not familiar with it you can read about it here.
There is a third optional parameter lets you change the behavior of the function so that it returns data as a string -rather than sending it to your browser. This can be useful if you want to process the data in some way. If you -set the parameter to true (boolean) it will return data. The default behavior is false, which sends it -to your browser. Remember to assign it to a variable if you want the data returned:
+rather than sending it to your browser. This can be useful if you want to process the data in some way. If you +set the parameter to true (boolean) it will return data. The default behavior is false, which sends it +to your browser. Remember to assign it to a variable if you want the data returned:$string = $this->load->view('myfile', '', true);
--
cgit v1.2.3-24-g4f1b