📜 ⬆️ ⬇️

Introducing Kohana 3.0 - Part 3

Meet the third part of a series of articles on development with Kohana PHP V3 (KO3). Previous parts can be found under the tag " familiarity with kohana 3.0 ". This time it will be about creating templates.

In the second article, we looked at views, and now we will inherit the Controller classes, which will allow us to create a template. A template is a view that consists mostly of (X) HTML code.

Before we begin to fill in the files with code, create a new folder in “/ application / views /” and call it “templates”. Now in your favorite editor, create a clean document and paste in the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Language" content="en-us" /> <title><?php echo $title;?></title> <meta name="keywords" content="<?php echo $meta_keywords;?>" /> <meta name="description" content="<?php echo $meta_description;?>" /> <meta name="copyright" content="<?php echo $meta_copywrite;?>" /> <?php foreach($styles as $file => $type) { echo HTML::style($file, array('media' => $type)), "\n"; }?> <?php foreach($scripts as $file) { echo HTML::script($file, NULL, TRUE), "\n"; }?> </head> <body> <div id="container"> <?php echo $header;?> <?php echo $content;?> <?php echo $footer;?> </div> </body> </html> 

')
Save it as “default.php” in “/ application / views / templates /”.

As you can see, this code essentially looks like the types that we created in the second part, only in a more advanced and universal, and therefore suitable for use throughout the project version. I will stop at “foreach” later.

At the moment we have a template, but there is no indication to the system to do anything with it. So let's create a new file with the following contents:

 <?php defined('SYSPATH') or die('No direct script access.'); class Controller_DefaultTemplate extends Controller_Template { public $template = 'templates/default'; /** * Initialize properties before running the controller methods (actions), * so they are available to our action. */ public function before() { // Run anything that need ot run before this. parent::before(); if($this->auto_render) { // Initialize empty values $this->template->title = ''; $this->template->meta_keywords = ''; $this->template->meta_description = ''; $this->template->meta_copywrite = ''; $this->template->header = ''; $this->template->content = ''; $this->template->footer = ''; $this->template->styles = array(); $this->template->scripts = array(); } } /** * Fill in default values for our properties before rendering the output. */ public function after() { if($this->auto_render) { // Define defaults $styles = array('assets/css/reset.css' => 'screen'); $scripts = array('http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'); // Add defaults to template variables. $this->template->styles = array_reverse(array_merge($this->template->styles, $styles)); $this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts)); } // Run anything that needs to run after this. parent::after(); } } 


Save it as “defaulttemplate.php” in the “/ application / classes / controller /” folder.

In this script, we inherit the “Controller_Template” class and do three basic operations: we make some properties (variables) available to our methods (actions), assign default values ​​to them, and then associate them with variables from the template before rendering it. This is where the foreach () loop from the template starts working. It uses the static methods of the “HTML” helper class: one for loading CSS styles, the other for JavaScript files. These methods, executed in a loop, will create the correct HTML code for connecting the files. They accept both a URL and a relative path.

If you don’t know what an assistant class is, here’s a brief definition from the Kohana 2.x documentation:
Helpers are handy features that are designed to help you with development.

They are similar to library functions, but there is a slight difference. In the case of libraries, it is necessary to create an instance of the library class in order to use its methods. Helpers are static class methods that are automatically loaded by the framework, so there is no need to perform additional actions.

So back to the code. As you can see, there is a mention of “assets / css / reset.css” in it, so let's deal with it. Create an “assets” directory in the root folder and “css” in it. I copied the styles from the article “ Create The Perfect CSS Reset ” and put it in the file “reset.css”, which I placed in “assets / css /.” You can also create directories “images”, “js” inside the folder “/ assets /” and “files”. This folder will be used to store static files.

Our application still doesn’t know what to do with all this, so let's change our controller. Open “/application/classes/controller/ko3.php” and replace the line:

 class Controller_Ko3 extends Controller 

on:

 class Controller_Ko3 extends Controller_DefaultTemplate 


You also need to bring the “index” action (action_index () method) to the following form:

 public function action_index() { $ko3_inner = array(); $ko3 = array(); $this->template->title = 'Kohana 3.0'; 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(); $this->template->content = View::factory('pages/ko3', $ko3); } 


Save the file. You may have noticed that the line “$ this-> template-> title = 'Kohana 3.0 ′;” was added, which will assign the value of the variable “title” in our templates. We also got rid of the “render ()” method at the end. It is no longer necessary to call it, because the “factory ()” method automatically renders the view into the “content” variable of the template. Pretty simple, right?

Before loading the page in the browser, it is probably worth removing some code that is no longer needed. Open “ko3.php”, which is in “application / views / pages /”, and bring it to this view:

 <h1>This is my first view</h1> <?php echo $content;?> <?php echo $ko3_inner; ?> <br/><?php echo $x;?> 


Now, having updated the page in the browser, you should see that it practically has not changed, not counting the appearance of the “Kohana 3.0 ″ header, but the source code has become completely different.

You may not understand the purpose of the remaining variables in the template, so let's go back to the action “index” and replace it with the following code:

 public function action_index() { $ko3_inner = array(); $ko3 = array(); $this->template->title = 'Kohana 3.0'; $this->template->meta_keywords = 'PHP, Kohana, KO3, Framework'; $this->template->meta_description = 'A test of of the KO3 framework'; $this->template->styles = array('assets/css/red.css' => 'screen'); $this->template->scripts = array('assets/js/jqtest.js'); 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(); $this->template->content = View::factory('pages/ko3', $ko3); } 


Pretty simple. You may have noticed that I did not fill out the page header and footer. But, I am sure, you will guess what needs to be done here. Hint: visualize the view in that variable (emoticon).

You may also notice that I connected “assets / css / red.css” and “assets / css / jqtest.js”. Let's fill them in now, starting with “/assets/css/red.css”:

 h1 { color: #FF0000; } 


Now “/assets/js/jqtest.js”:

 $("document").ready(function() { alert('Hello Kohana!'); }); 


Save them and update the site. A popup window should appear and the first line should turn red.

Congratulations, in this part you have created your controller using the template file you created on the basis of the built-in controller “Template”.

Source: https://habr.com/ru/post/111537/


All Articles