Skip to content

Handling placeholders

Rosario Carvello edited this page Jan 16, 2018 · 45 revisions

Introduction

Consider the common situation where you have to manage and show data dynamically on the web pages. You just learned that showing output is the main role of the View. This is still valid also when data to show are dynamical. For this purpose, in this paragraph, we expose a solution for handling dynamic web page by a using static Template, View, and Controller

Handling placeholders by using Template, View, and Controller

Suppose we have the following templates\home.html.tpl file just shown on the previous page, in which we have placed the static placeholder {UserName}. Now we want to assign to it a value, eg. 'Mark', dynamically provided at run-time.

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

To do this job we procede by creating the foolowing View class we call views\Home.php.

<?php
namespace views;
use framework\View;

class Home extends View
{
    /* Contructor.
     * Override framework\View __construct() to 
     * use templates\home.html.tpl
     */
    public function __construct($tplName = null)
    {
        $tplName = "home";
        parent::__construct($tplName);
    }
    
    /* Custom method designed to manage the dynamic content 
     * of the placeholder {UserName}.
     *
     * @param string $name The name value to assign to {UserName}
     */
    public function setUserName($name)
    {
        // setVar is a method inherited from framework\View
        $this->setVar("UserName",$name);
    }
    
}

Note that, unlike the abstract framework\View instantiated to show the static content that was discussed in the previous example, we are now creating the class, views\Home.php, that extends framework\View. We create it for two reasons:

  • for implementing the (specializing) setUserName($name) method, which dynamically manages the value of the placeholder contained in its own template templates\home.html.tpl
  • to avoid calling the setVar method directly from within the controllers\Home because the setVar method is a framework\View responsibility

Then we modify the controller controllers\Home.php as follow to enable it to communicate with views\Home.php (pay attention to the comments):

namespace controllers;

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

class Home extends Controller
{
    /**
     * Home constructor.
     * @override framework\Controller __construct()
     */
    public function __construct()
    {
        /**
         * A reference to the file: templates/home.html.tpl
         * @note Do not to specify the file extension ".html.tpl".
         */
        $tplName = "home";


        /**
         * Set the view with a new object of type framework\View. 
         * @note: We create the HomeView object by using the 
         *        template reference.
         */
        $this->view = new HomeView($tplName);

        /**
         *  To starting up the standard WebMVC behavior of acting
         *  cooperation between Controller and the View just
         *  invoke the parent constructor by passing the current view  
         */
        parent::__construct($this->view);
        
        /**
         * By default, the controller behavior will be 
         * that to greet a Guest user
         */
        $this->view->setUserName("Guest");

    }

    /**
     * Specifies the user for greeting
     * It wraps a call to $view->setUserName($name) 
     *
     * @param string $user User name value to be greet
     */    
    public function userToGreet($user) {
        $this->view->setUserName($user);
        $this->render();
    }
    
}

The following figure shows the WebMVC directory tree with files location:

WebMVC Files

Note we used 'Home' name for naming Controller, View, and 'home' for Template. We speak about this naming convention

Finally, run the controller by typing the following address into your web browser:

http://localhost/home/greetings/Mark

You should see the page:

Welcome to the site Home Page

Welcome Mark

You can also run the controller without calling its greetings() method. Type:

http://localhost/home/

Now, you should see:

Welcome to the site Home Page

Welcome Guest

Insight: General considerations

Although this example is very simple, there are still many feasible considerations to do:

What setVar() exactly does ?

The method setVar($placeholder, $value) provided by framework\View find a given placeholder in the active template and replace it with a given value. Bear in mind that:

  • If many occurrences of placeholders (like {UsenName}) are designed in a template, then they will be all replaced in one single setVar() call.
  • You must call a setVar() method after the view has been initialized in the controller.
  • You must not call twice the setVar() method by giving it, each time, the same placeholder.This because after the first time the placeholder will be replaced with a value, then, at the second call of setVar(), no placeholder will be found and an exception will be thrown.

Follow the complete reference for setVar():

    /**
     * Replaces all occurrences of the given placeholder with a given value.
     * If the placeholder is contained into a (previously opened) block
     * the replacement is made against the opened block.
     *
     * @param string $placeholder  The placeholder to find
     * @param string $value        The value for replacing to founded placeholder
     * @param bool|true $useBraces If true (default) the placeholder name contain braces {}
     * @param bool $xssPurify      If true (default=false) purify the value against XSS
     * @param string $charset      Charset string. Default is the CHARSET constant value 
     *                             (default UTF8) defined into config\application.config.php
     * 
     * @throws VariableNotFoundException.  If variable/placeholder was not found
     * @throws NotInitializedViewException If template was not loaded
     */
    public function setVar($placeholder, $value, $useBraces=true, $xssPurify=false, $charset = CHARSET)

What about the use of render()?

// TODO

Whats next

//TODO

Clone this wiki locally