PHP view
1Presentation
The PHP view allows you to use templates written in plain PHP.
This view does not require any external library. Template files are simple PHP files that are included at rendering time.
Template variables are made available as PHP variables directly accessible in the templates.
To use this view, declare it as the default view in the etc/temma.php configuration file:
<?php
return [
'application' => [
'defaultView' => '\Temma\Views\Php',
]
];
2Template files and inclusion
Template files are simple PHP files, which contain HTML code mixed with PHP instructions.
You can segment your pages by including other PHP files using the
include or require instructions.
For example, suppose you have two actions, list and show, which display
different pages using the list.php and show.php files, respectively.
If these two pages display the same page header, it is better to separate it so that you do not have to copy it on each page.
So you could have the header.php file, which would look like this:
<html>
<head>
<title>Generic page title</title>
</head>
<body>
Our two page templates would then look like this:
<!-- file list.php -->
<?php include('header.php'); ?>
<h1>LIST</h1>
</body>
<!-- file show.php -->
<?php include('header.php'); ?>
<h1>SHOW CONTENT</h1>
</body>
3Variables
Template variables defined in the controller are directly accessible in templates
as PHP variables.
Imagine that the controller contains the following code:
$this['name'] = 'Anakin';
You can display the value of the name variable in your template:
<?=$name?>
This will have the effect of displaying the following text:
Anakin
4Lists
Let's say your controller sets a template variable that contains a list:
$this['fruits'] = [
'orange',
'banana',
'strawberry',
];
You can easily display any of the values in the list:
<?=$fruits[2]?>
And you will get:
strawberry
5Associative arrays and objects
It is possible to display the content of an element of an associative array from its key, or of an attribute of an object from its name. For example:
$this['colors'] = [
'red' => '#ff0000',
'green' => '#00ff00',
'blue' => '#0000ff',
];
To display a color, just write:
<?=$colors['red']?>
Which will give:
#ff0000
6Conditions
To do conditional processing, you use PHP's if / else statements.
For example, if your controller sets a template variable containing information for a user:
$user = $this->_dao->get($userId);
$this['user'] = $user;
You will then be able to display a link only if the user is an administrator:
<?php if ($user['roles']['admin']): ?>
<a href="/user/show/<?=$user['id']?>">View my account</a>
<?php endif; ?>
Or:
<?php if ($user['roles']['admin']) { ?>
<a href="/user/show/<?=$user['id']?>">View my account</a>
<?php } ?>
7Loops
Very often, you need to apply processing to a group of items. This usually takes the form of a list of items retrieved
from the database, which you want to display one after the other.
To do this, you use PHP's foreach instruction.
Imagine the controller retrieves a list of users from the database:
$users = $this->_dao->search();
$this['users'] = $users;
It's very easy to loop through all users to display their name:
<ul>
<?php foreach ($users as $user): ?>
<li><?=$user['name']?></li>
<?php endforeach; ?>
</ul>
Or:
<ul>
<?php foreach ($users as $user) { ?>
<li><?=$user['name']?></li>
<?php } ?>
</ul>
8Escaping
Unlike the Smarty view, the PHP view does not provide auto-escaping of variables. If a variable contains special characters ("<", ">", "&", ...), you must not write it as is in the template, otherwise you will generate a non-compliant HTML stream or an XSS security vulnerability.
To escape special characters, you must use the PHP htmlspecialchars() function:
<!-- escaped variable -->
<?=htmlspecialchars($name)?>
<!-- non-escaped variable (use only if content is trusted) -->
<?= $name ?>
It is recommended to systematically escape all variables displayed in templates, for example by using a utility function:
<?php
function e($value) {
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
?>
<p><?=e($name)?></p>
<p><?=e($email)?></p>