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