3.1 Dynamic content - notafrancescodavid/webmvcframework GitHub Wiki

The code described in the previous example shows in the browser the static data contained in the file home.html.tpl. Now, consider the typical situation where you have to manage data dynamically in the view. WebMVC let you design a custom View class, that extends framework\View, and that will be responsible for managing the corresponding template.

The concept of placehoder

In the following HTML code we augment the previous version of home.html.tpl. Now, together with the static content, we want to show 'Welcome {PersonName}' where {PersonName} can assume different values. With {PersonName} we declare a placeholder, a string delimited by braces and located somewhere in the template, that we want to substitute with another string. Here it is the code of the new home.html.tpl in which we wish to replace the placeholder {PersonName} with the name Mark.

<!DOCTYPE html>
<html>
  <head>
    <title>Site home page</title>
  </head>
  <body>
    <p>Welcome to the site Home Page</p>
    <br />
    <p>Welcome {PersonName}</p>
  </body>
</html>

We shall use the same class name, that is Home, for both the controller and the view that we need to run the example; the same convention will be applied to the model class in the following sections. The unique identification of a name is possible using the concept of namespace; this convention allows avoiding the proliferation of names or naming conflicts in complex projects. See the positioning of the Home classes for the controller and the view, as well as the template home.html.tpm, in the directory hierarchy of WebMVC.

The setVar method

The code of the Home view class, written to handle the dynamic aspects of a page, contains the setVar () method inherited from the framework\View class. In this example, the method setvar() is used to replace the placeholder {PersonName} with the name of a person represented by the parameter $name of setPersonName($name).

<?php
namespace views;
use framework\View;

class Home extends View
{
    public function __construct()
    {
       $tplName = "home";
       parent::__construct($tplName);
    }

    public function setPersonName($name){
        // setVar is a method inherited from the framework\View class
        $this->setVar("PersonName",$name);
    }  
}

At runtime, by invoking setPersonName("Mark"), the static HTML will be dynamically modified as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>Site home page</title>
  </head>
  <body>
    <p>Welcome to the site Home Page</p>
    <br />
    <p>Welcome Mark</p>
  </body>
</html>

Bear in mind that:

  • If multiple placeholders {PersonName} are in a single HTML file then they will be all replaced in one single setVar code.
  • You must call a setVar method after the view has been initialized in the controller.
  • You must not call two times the same setVar, because if no placeholder will be found then an exception will be thrown.

Finally, we rewrite our Home controller class to call the setPersonName($name) method.

The controller provides the method sayWelcome in order to accept the sayWelcome command from the user's browser. The management of the template is now done by the user-defined view HomeView.

<?php

namespace controllers;

use framework\Controller;
use views\Home as HomeView;

class Home extends Controller
{  
    public function __construct() {
        // set the view variable to an instance of the HomeView class
        $this->view = new HomeView();
        parent::__construct($this->view);
    }

    public function sayWelcome($name) {
        $this->view->setPersonName($name);
        $this->render();
    }
}

You can run the method sayWelcome() with a parameter to say Welcome to a person. Simply type from URL the following address:

http://localhost/home/say_welcome/Mark , and then try a second invocation with the parameter's value John:

http://localhost/home/say_welcome/John

Your output will be:

Welcome Mark
Welcome John

Next section: dynamic block

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