3.2 Dynamic block - notafrancescodavid/webmvcframework GitHub Wiki

Blocks

We can extend the idea of dynamic substitution of a placeholder with a single name to the case where we have to show a list of names. With the framework, you can use the concept of block. A block is nothing more than a piece of HTML code delimited by two comments that mark its endpoints. A block is usually subject to a transformation in order to render dynamic content in the browser. The management of blocks is a useful feature, for example, when we have to render a list of records coming from a database.

How can you create a block? Suppose that we have a list of people whose names have to be shown. The file user_list.html.tpl provides the static structure of a table in which there exist two placeholders to change: {FirstName} and {LastName}.

<html>
<head>
    <title>UserList</title>
</head>

<body>
    <h3>This example shows Blocks usage for data repetition inside a static GUI</h3>
        <table>
            <tr>
                <th>FIRST NAME</th>
                <th>LAST NAME</th>
            </tr>

            <!-- BEGIN Users -->
            <tr>
                <td>{FirstName}</td>
                <td>{LastName}</td>
            </tr>
            <!-- END Users -->
        </table>
    </body>
</html>

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 UserListView and UserListController in order to show a list of people names.

<?php
namespace views;

use framework\View;

class UserListView extends View
{
    public function __construct($tplName = null)
    {
        if (empty($tplName)) {
            $tplName = "/user_list";
		}
        parent::__construct($tplName);
    }

    public function setUserBlock($userList)
    {
        $this->openBlock("Users");
        foreach ($userList as $user){
            $this->setVar("FirstName",$user["FirstName"]);
            $this->setVar("LastName",$user["LastName"]);
            $this->parseCurrentBlock();
        }
        $this->setBlock();
    }
}

We have created a method in the view that uses the block Users and processes it with all the values contained into the $userList received as a parameter; specifically, we:

  • Consider the block Users by invoking the openBlock method inherited from framework\View. This means that, from now, each future action of the setVar method will be restricted to the HTML contained inside the block Users.

  • Then, loop inside $userList and for each element we:

    1. Call the setVar method to replace the placeholders {FirstName} and {LastName} with the value represented by the current element referenced by $userList;
    2. Call the parseCurrentBlock() to instruct the View to process the opened block (that contains {FirstName} and {LastName}). Then, if further people remain in $userLIst, the method arranges the things for the next block. This means that the placeholders contained in the block will be valorized, by invoking once again setVar, as long as there is a couple of name and surname in $userList.
  • By calling the setBlock method, we close the active opened block; as a result, its content will result dynamically valorized and ready to be shown.

<?php
namespace controllers;

use framework\Controller;
use views\UserListView as UserListView;

class UserListController extends Controller
{
    public function __construct() {
      $this->view = new UserListView();
	  parent::__construct($this->view);
    }

    public function showUsers() {
        $users = array (
            array("FirstName" => "John", "LastName" => "Red"),
            array("FirstName" => "Mark", "LastName" => "White"),
            array("FirstName" => "Diana", "LastName" => "Brown"),
        );
		$this->view->setUserBlock($users);
		$this->render();
    }
} 

Now you can run the controller's method showUsers to get a list of people!

localhost/user_list_controller/show_users

In this section, we have seen how to instantiate a controller and a view, and how to link them. In an MVC architecture, the third component is the model. In the next page we shall describe what is a model and how to use it properly in the context of the WebMVC framework.

⚠️ **GitHub.com Fallback** ⚠️