Introducing Kohana 3.0 - Part 2

Original author: ellisgl
  • Transfer
Meet the second part of a development article series with Kohana PHP V3 (KO3). The first is here if you have not read it yet. This time we will go over the development of types (representations).

In the “application” directory, create a new “views” folder, and inside it the “pages” folder. Now create a new document in the editor and paste there:

 
  Hello!
 
 
  

This is my first view


 

Save this as “ko3.php” in the “application / views / pages” directory. So far, this is a simple HTML code, but soon we will add a little PHP. Let's open the “ko3 ″ (” application / classes / controller / ko3.php ”controller). Replace the action_index method with the following:
public function action_index ()
 {
    $ this-> request-> response = View :: factory ('pages / ko3');
 }

Save the file and open in your browser “http: // localhost / mykohana3 / ko3”. The line “This is my first view” should appear. The code above is quite simple, we use the “Factory” method of the “View” class to download the file “application / views / pages / ko3.php”, visualize (render) and display it. Not too exciting, so let's go back to the view (”application / views / pages / ko3.php”) and add:

After the tags “h1 ″. The view should become like this:

 
  Hello!
 
 
  

This is my first view


  
 

If we now refresh the page in the browser, we will see an error related to an undefined variable. So let's fix this by casting the Action_Index method to the following form:
public function action_index ()
 {
    $ view = View :: factory ('pages / ko3');
    $ view-> content = 'We have data!';
    $ this-> request-> response = $ view-> render ();
 }

And now, having updated the browser, we will see the inscription “This is my first view”, and under it “We have data!”. Let's go through the code line by line.
$ view = View :: factory ('pages / ko3');

This line loads our view (”application / views / pages / ko3.php”) into the controller.
$ view-> content = 'We have data!';

Here we set the variable “content” (which can be used in the view file) to “We have data!”
$ this-> request-> response = $ view-> render ();

The render () method renders the view, and $ this-> request-> response displays it.

Simple enough, but there is another way to do the same:
public function action_index ()
 {
    $ data ['content'] = 'We have data!';
    $ view = View :: factory ('pages / ko3', $ data);
    $ this-> request-> response = $ view-> render ();
 }

During visualization, variables in the form are replaced by array elements with the corresponding keys. You just need to remember to pass this array as the second parameter to the “Factory” method.

But that is not all! There are two other ways left to bind the view variables and the controller.
public function action_index ()
 {
    $ view = View :: factory ('pages / ko3')
                    -> set ('content', 'We have data!');
 
    $ this-> request-> response = $ view-> render ();
 }

Here, the “set” method of the “View” class is used, with which you can define all the variables used in the view.
Fourth way:
public function action_index ()
 {
    $ content = 'We have data!';
    $ view = View :: factory ('pages / ko3')
                    -> bind ('content', $ content);
    $ this-> request-> response = $ view-> render ();
 }

Above, the “bind” method of the “View” class is used. It differs from the “set” method from the third method in that instead of directly assigning a value to a variable in the form, it associates it with some variable in the controller by reference.
That is, if we do this:
public function action_index ()
 {
    $ content = 'We have data!';
    $ view = View :: factory ('pages / ko3')
                    -> bind ('content', $ content);
    $ content = 'Our data changed';
    $ this-> request-> response = $ view-> render ();
 }

Then instead of “We have data!” “Our data changed” will appear on the screen.

Now let's pretend in the form! In the directory “application / views /” create a new folder “blocks”. Open a new document in the editor and place in it:

This is an inner view


Save this as “ko3_inner.php” in the newly created “application / views / blocks /”. Now let's edit the view “ko3 ″ (” application / views / pages / ko3.php ”), adding after:

following line:
render (); ?>

Our view should now look like this:

 
  Hello!
 
 
  

This is my first view


  
  render (); ?>
 

If we refresh the page, then “This is an inner view” will be added to the previously displayed inscriptions. Thus, it is convenient to use static content, but we will not be able to use variables directly in that form. To fix this, let's go back to our controller (“application / classes / controllers / ko3.php”) and bring the “Action_Index” method to the following form:
public function action_index ()
 {
    $ ko3_inner ['content'] = 'We have more data';
    $ ko3 ['content'] = 'We have data';
    $ ko3 ['ko3_inner'] = View :: factory ('blocks / ko3_inner', $ ko3_inner)
                                   -> render ();
    $ view = View :: factory ('pages / ko3', $ ko3);
    $ this-> request-> response = $ view-> render ();
 }

This will put the rendered view “blocks / ko3_inner.php” into the $ ko3 array, which will then be rendered in the main view “pages / ko3.php”, which we now need to change. Line:
render (); ?>

which we added earlier, change to:

The view should now look like this:

 
  Hello!
 
 
  

This is my first view


  
  
 

It remains to change the internal view ("application / views / blocks / ko3_inner.php"), bringing it to this form:

This is an inner view



If you refresh the page in the browser after saving the file, you should see the following:
This is my first view
 
We have data
 
This is an inner view
 
We have more data

Not bad, now you can make more flexible views ready for reuse.

Now we’ll learn how to make global variables that will be available to your views. Let's go back to the controller (”applications / classes / controllers / ko3.php”) and add the line at the very beginning of the “Action_Index” method:
View :: set_global ('x', 'This is a global variable');

To make the method look like this:
public function action_index ()
 {
    View :: set_global ('x', 'This is a global variable');
 
    $ ko3_inner ['content'] = 'We have more data';
    $ ko3 ['content'] = 'We have data';
    $ ko3 ['ko3_inner'] = View :: factory ('blocks / ko3_inner', $ ko3_inner)
                                   -> render ();
    $ view = View :: factory ('pages / ko3', $ ko3);
    $ this-> request-> response = $ view-> render ();
 }

Now, if you add in the views:


you should see the text “This is a global variable” on the page.

You can also make global references using the “View :: bind_global” method and these variables will be available to all instances of the “View” object.

Also popular now: