Meet the second part of a series of articles on development with Kohana PHP V3 (KO3). The first is
here , in case you haven't read it yet. This time we will go through the development of species (views).
In the “application” directory, create a new “views” folder, and inside it a “pages” folder. Now create a new document in the editor and paste there:
< html >
< head >
< title > Hello ! </ title >
</ head >
< body >
< h1 > This is my first view </ h1 >
</ body >
</ html >
Save it as “ko3.php” in the “application / views / pages” directory. So far there is a simple HTML code, but soon we will add some PHP. Let's open the “ko3 ″ controller (“ application / classes / controller / ko3.php ”). Replace the “action_index” method with the following:
public function action_index ( )
{
$ this -> request -> response = View :: factory ( 'pages / ko3' ) ;
}
Save the file and open the browser “http: // localhost / mykohana3 / ko3”. The line “This is my first view” should appear. The code above is pretty simple, we use the “Factory” method of the “View” class to load the “application / views / pages / ko3.php” file, to render (render) and display it. Not too exciting, so back to the view ("application / views / pages / ko3.php") and add:
<? php echo $ content ; ?>
After tags “h1 ″. The view should be like this:
<html>
<head>
<title> Hello! </ title>
</ head>
<body>
<h1> This is my first view </ h1>
<? php echo $ content ; ?>
</ body>
</ html>
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 bringing 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, updating the browser, we will see the inscription “This is my first view”, and under it “We have data!”. Let's go line by line by code.
$ 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 assign the variable “content” (which can be used in the view file) to the value “We have data!”
$ this -> request -> response = $ view -> render ( ) ;
The render () method renders the view, and $ this-> request-> response displays it.
Simple enough, 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 with elements of the array with the corresponding keys. You just need to remember to transfer this array to the “Factory” method with the second parameter.
But that's not all! There are two other ways left to associate the view and controller variables.
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 variables used in the form.
Fourth way:
public function action_index ( )
{
$ content = 'We have data!' ;
$ view = View :: factory ( 'pages / ko3' )
-> bind ( 'content' , $ content ) ;
$ this -> request -> response = $ view -> render ( ) ;
}
The above is the “bind” method of the “View” class. It differs from the “set” method of the third method in that instead of directly assigning the value of 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!” The inscription “Our data changed” appears on the screen.
Now let's pretend to view! In the directory “application / views /” create a new folder “blocks”. Open a new document in the editor and place in it:
< h3 > This is an inner view </ h3 >
Save it as “ko3_inner.php” in the newly created “application / views / blocks /”. Now let's edit the “ko3 ″ (” application / views / pages / ko3.php ”) view, adding after:
<? php echo $ content ; ?>
following line:
<? php echo View :: factory ( 'blocks / ko3_inner' ) -> render ( ) ; ?>
Our view should now look like this:
<html>
<head>
<title> Hello! </ title>
</ head>
<body>
<h1> This is my first view </ h1>
<? php echo $ content ; ?>
<? php echo View :: factory ( 'blocks / ko3_inner' ) -> render ( ) ; ?>
</ body>
</ html>
If we refresh the page, then “This is an inner view” will be added to the previously displayed captions. Thus, it is convenient to use static content, but we will not be able to use variables directly in that form. To fix this, 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 place the rendered view of “blocks / ko3_inner.php” in the $ ko3 array, which will then be rendered in the main form of “pages / ko3.php”, which we now need to change. Row:
<? php echo View :: factory ( 'blocks / ko3_inner' ) -> render ( ) ; ?>
which we added earlier, change to:
<? php echo $ ko3_inner ; ?>
The view should now look like this:
<html>
<head>
<title> Hello! </ title>
</ head>
<body>
<h1> This is my first view </ h1>
<? php echo $ content ; ?>
<? php echo $ ko3_inner ; ?>
</ body>
</ html>
It remains to change the internal view ("application / views / blocks / ko3_inner.php"), bringing it to this view:
<h3> This is an inner view </ h3>
<? php echo $ content ; ?>
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.
And now we will 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 a 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 to views:
<br/> <? php echo $ x ; ?>
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.