How To Use Views - ApinePHP/legacy-framework GitHub Wiki
Views represents the response to a request made by the client. They contain the header information of the response, the processed data from the controller. Views then re-format the data to the type of output needed. There are currently four types of views :
- HTML Views : These views are represented as HTML pages implementing our Template Language;
- PHP Views : These views are represented as HTML pages generated by PHP;
- Twig Views : These views implement the Twig templating engine;
- JSON Views : These views are represented as JSON arrays;
- File Views : These views files from the file system;
- Redirection Views : Move to another page on the website or redirect to a page on another domain.
The most useful type is the HTML view as it represent a html document to display in the clients browser. This page focuses on basic features and HTML views.
It is possible to modify information of the response using Views. It is possible to enter custom header rules and set the response code.
To add a header rule to the view use the set_header_rule method with as an argument a valid HTTP Header rule. To change the response code use the set_response_code method.
// Set a header rule
$view->set_header_rule(‘Location: https://google.com’);
// Change the response code
$view->set_response_code(202);
Parameters are the data to be returned to the client. In a HTML view, these are values available to be displayed using the template engine. In a JSON, these are returned as a JSON array. Values may be of any types. In JSON views, objects are serialized. However in HTML views, the template can only manage basic types like strings, integers, floats and arrays.
$view->set_param(‘current_year’, 2016);
HTML views are divided into two parts : the layout and the view. The layout is the part in which the view part is inserted. The layout may contain every part of the HTML page that may be reused by a many of pages like the header and the footer. The view is linked in the layout using a tag. These are HTML files located preferably in the views directory.
Layouts must be located in views/layouts.
The view and layouts file may be set with the set_view and set_layout methods. They accept a path to .html files from the views directory. The path must not included the extension of the file name.
APIne’s default HTMLView class implements the TinyTemplate template engine. Its template language uses tags delimited by curly brackets {} to define rules. They allow for many function and conditional expressions to be used in views.
The most important tag is the {yield} tag which allow to bind a template into a layout template. Here’s other useful tags:
-
{apine_view_title}: The title of the page as defined in the view class using theset_title()method; -
{apine_config:section,name}: A short-hand to get value from the main config file; -
{apine_translation:prefix,key}: A short-hand to get a translation string from the current locale; -
{apine_url_path:home/index}: A short-hand to write a url to another page on the same application; -
{variable_name}: Get the value of a parameter passed to the view.
For more information about the template engine, check the section about the [Template module].
Let’s say we want to return the view “title” with the “simple” layout as shown below.
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>{apine_view_title}</title>
</head>
<body>
{yield}
</body>
</html><h1>A small example of how views work</h1>
<a href=”{apine_url_path:login}”>Login</a>public function title () {
$view = new Apine\MVC\HTMLView();
$view->set_title(‘Title’);
$view->set_layout(‘simple’);
$view->set_view(‘title’);
return $view;
}PHP views are the most flexible views available. They allow to use native PHP as a templating language. They work in a similar way as the HTML views - they are separated in to main parts: the view and the layout.
The Twig views are very similar to the HTML views. The only difference is that they also accept .twig files.
You can create a new Twig view by instantiating the TwigView class. It does not have any mandatory parameters.
See the documentation on Twig for details on the syntax of Twig
$view = new Apine\MVC\TwigView();
$view->set_view('twig_home');
$view->set_layout('twig_layout');JSON views represent a response with a json string as its content. It takes a valid json string or an array as a optional parameter of the constructor. It is possible to reassign the json object by calling the set_json_file() method. The method takes a valid json string or an array.
It is also possible to add or edit data from the json object with the set_param() introduced above.
$view = new Apine\MVC\JsonView();
$view->set_json_file([
'string' => 'value'
]);
$view->set_param('string', 'other value');File view as the name implies represent a response whose body is the content of a file. The method set_file() set the file to send to the client. It must be the path to the file or an instance of Apine\File\File.
$view = new Apine\MVC\JsonView();
$view->set_file('/path/to/file');Redirection views are very minimal views. They are designed for custom redirection to external resources or other page on the same domain. Everything is done by manually setting the headers to the view.
$view = new Apine\MVC\RedirectionView();
$view->set_header_rule(‘Location: https://google.com’);
$view->set_response_code(301);