From 9607e738fc65f0dc3b70be810c6d2c3dc5060f87 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 5 Oct 2011 16:42:42 -0500 Subject: fixed code block spacing on URLs and Views general docs --- user_guide_src/source/general/views.rst | 132 +++++++++++++++++++++++++------- 1 file changed, 105 insertions(+), 27 deletions(-) (limited to 'user_guide_src/source/general/views.rst') diff --git a/user_guide_src/source/general/views.rst b/user_guide_src/source/general/views.rst index cb60ce20f..7d0accafd 100644 --- a/user_guide_src/source/general/views.rst +++ b/user_guide_src/source/general/views.rst @@ -20,10 +20,17 @@ Creating a View =============== Using your text editor, create a file called blogview.php, and put this -in it: - - My Blog

Welcome to my -Blog!

+in it:: + + + + My Blog + + +

Welcome to my Blog!

+ + + Then save the file in your application/views/ folder. Loading a View @@ -37,10 +44,18 @@ 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: +replace the echo statement with the view loading function:: + + load->view('blogview'); + } + } + ?> -load->view('blogview'); } } ?> If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this:: @@ -55,8 +70,21 @@ 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:: - load->view('header');       $this->load->view('menu');       $this->load->view('content', $data);       $this->load->view('footer');    } } ?> + load->view('header'); + $this->load->view('menu'); + $this->load->view('content', $data); + $this->load->view('footer'); + } + } + ?> In the example above, we are using "dynamically added data", which you will see below. @@ -77,25 +105,49 @@ Data is passed from the controller to the view by way of an **array** or an **object** in the second parameter of the view loading function. Here is an example using an array:: - $data = array(                'title' => 'My Title',                'heading' => 'My Heading',                'message' => 'My Message'           ); $this->load->view('blogview', $data); + $data = array( + 'title' => 'My Title', + 'heading' => 'My Heading', + 'message' => 'My Message' + ); + + $this->load->view('blogview', $data); And here's an example using an object:: - $data = new Someclass(); $this->load->view('blogview', $data); + $data = new Someclass(); + $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:: + + load->view('blogview', $data); + } + } + ?> -load->view('blogview', $data); } } ?> Now open your view file and change the text to variables that correspond -to the array keys in your data: +to the array keys in your data:: + + + + <?php echo $title;?> + + +

+ + - <?php echo $title;?> -

Then load the page at the URL you've been using and you should see the variables replaced. @@ -107,18 +159,44 @@ variables. You can pass multi dimensional arrays, which can be looped to generate multiple rows. For example, if you pull data from your database it will typically be in the form of a multi-dimensional array. -Here's a simple example. Add this to your controller: +Here's a simple example. Add this to your controller:: + + load->view('blogview', $data); + } + } + ?> + +Now open your view file and create a loop:: + + + + <?php echo $title;?> + + +

+ +

My Todo List

+ + - <?php echo $title;?> -

My Todo List

+ + .. 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 -- cgit v1.2.3-24-g4f1b