-
Notifications
You must be signed in to change notification settings - Fork 16
Handling blocks
We can extend the idea of dynamic substitution of a placeholder with a value to the case where we have to handle a block of content and we need to perform some actions on it. For this purpose, WebMVC uses the concept of Block.
A Block is nothing more than a piece of code (typically HTML but it can be also Javascript or CSS) delimited by two comments that mark its endpoints. A content inside a block is usually subject to an action in order to render a dynamic page in the browser. The management of blocks is a useful feature, for compute dynamically on:
- content repetition and/or valorization
- content replacement
- content hiding .
Supposing we want to show a list of users rather than a single one like we previously done in the controllers\Home
. On the section about Dynamic Content, we introduced and coded the following templates\user_manager.html.tpl, by using a block named Users suitable for this purpose.
<!DOCTYPE html>
<html>
<head>
<title>Users list</title>
</head>
<body>
<h1>Users list</h1>
<table>
<thead>
<tr>
<th>User</th>
<th>Email</th>
<tr>
</thead>
<tbody>
<!-- BEGIN Users -->
<tr>
<td>{UserName}</td>
<td>{UserEmail}</td>
</tr>
<!-- END Users -->
</tbody>
</table>
</body>
</html>
As you can guess from the HTML code, the expected dynamic behavior of the web page will be to show a table containing some users. For each user, it will show the username and email.
This means that the HTML contained in the Users block can be considered as a template pattern that must be dynamical repeated N times, and valued each time with different values representing a user.
In the HTML code, two comments have been added, and they together wrap the block of code that will be dynamically replaced many times in the HTML file. These comments mark the BEGIN and the END of a block of name Users
. This means that WebMVC will be able to recognize the block.
Now, we can write the code of views\UserManager
and controllers\UserManager
in order to show a table of users.
<?php
namespace views;
use framework\View;
class UserManager extends View
{
public function __construct($tplName = null)
{
if (empty($tplName)) {
$tplName = "/user_manager";
}
parent::__construct($tplName);
}
public function setUserBlock($users)
{
$this->openBlock("Users");
foreach ($users as $user){
$this->setVar("UserName",$user["UserName"]);
$this->setVar("UserEmail",$user["UserEmail"]);
$this->parseCurrentBlock();
}
$this->setBlock();
}
}
In the view we have created a method setUserBlock
that uses the block Users
and processes it with all the elements contained in the given $users
array; specifically, we:
-
Invoking the
openBlock
method inherited fromframework\View
. This method, behind the scenes, do:- Recognizes the block Users
- Creates a swap, initially empty, variable to store the dynamic content to be generated. We refer it as
dynamic_content
- Generates an internal template that contains the HTML eclosed in the Users block. We refer it as
template_pattern
- Predisposes each future calling of the
setVar
method to be restricted to the content oftemplate_pattern
.
-
Then, we loop inside
$users
and, for each element:- We call the
setVar
method to valorize the placeholders{UserName}
and{UserEmail}
with the value referenced by the array keysUserName
andUserEmail
of current element ($user
) of$users
- We call the
parseCurrentBlock()
which, behind the scenes do:- Concatenates the just valorized content of
template_pattern
to the content ofdynamic_content
. By doing so the content of dynamic_content, initially null, will be greater at each loop. At the end of the loop it will contain all the users data rows. - Regenerates the
template_pattern
- Concatenates the just valorized content of
- Then:
- If there are more elements in
$users
, PHPforeach
loop back to i. This means that the placeholders contained in the (just regenerated)template_pattern
will be ready to be valorized once more with the next element of$users
- else PHP
foreach
exit from the loop
- If there are more elements in
- We call the
-
Finally, by calling the
setBlock
method, we stop the processing on the opened block and, behind the scenes, method replaces the text originally enclosed in the blockUsers
with the text contained into its internal variabledynamic_content
//TO BE CONTINUED
In this section, we show how handling the content repetition and valorization of a block. What we have done is:
$users = array(
array('UserName'=>'Mark', 'UserEmail'=>'[email protected]'),
array('UserName'=>'Elen', 'UserEmail'=>'[email protected]'),
array('UserName'=>'John', 'UserEmail'=>'[email protected]')
);
2. We built a static GUI design templates\user_manager.html,tpl
, containing a block and two placeholders:
User | |
---|---|
<!-- BEGIN Users --> | |
{UserName} | {UserEmail} |
<!-- END Users --> |
4. We build the custom class controllers\UserManger
for handling the view as well as the HTTP requests
User | |
---|---|
Mark | [email protected] |
Elen | [email protected] |
John | [email protected] |
// TODO