About this example : Hello World with block

Controller

controllers/examples/cms/HelloBlock.php

<?php
/**
 * Class HelloBlock
 *
 * {ControllerResponsability}
 *
 * @package controllers\examples\cms
 * @category Application Controller
 * @author  {AuthorName} - {AuthorEmail}
*/
namespace controllers\examples\cms;

use framework\Controller;
use framework\Model;
use framework\View;
use models\examples\cms\HelloBlock as HelloBlockModel;
use views\examples\cms\HelloBlock as HelloBlockView;
use framework\components\DataRepeater;

class HelloBlock extends Controller
{
    protected $view;
    protected $model;

    /**
    * Object constructor.
    *
    * @param View $view
    * @param Model $mode
    */
    public function __construct(View $view=null, Model $model=null)
    {
        $this->view = empty($view) ? $this->getView() : $view;
        $this->model = empty($model) ? $this->getModel() : $model;
        parent::__construct($this->view,$this->model);
    }

    /**
    * Autorun method. Put your code here for running it after object creation.
    * @param mixed|null $parameters Parameters to manage
    *
    */
    protected function autorun($parameters = null)
    {
        $this->view->setVarWelcomeMessage("Hello World with block");
        $users = $this->model->getUsers();

        $this->view->openBlockUsers();
        foreach ($users as $user){
            $this->view->setBlockVarUsersName($user['Name']);
            $this->view->setBlockVarUsersLocation($user['Location']);
            $this->view->parseCurrentBlock();
        }
        $this->view->setBlock();

        /*
        $d = new DataRepeater($this->view,null,"Users",$users);
        $d->render();
        */
    }

    /**
    * Inizialize the View by loading static design of /examples/cms/hello_block.html.tpl
    * managed by views\examples\cms\HelloBlock class
    *
    */
    public function getView()
    {
        $view = new HelloBlockView("/examples/cms/hello_block");
        return $view;
    }

    /**
    * Inizialize the Model by loading models\examples\cms\HelloBlock class
    *
    */
    public function getModel()
    {
        $model = new HelloBlockModel();
        return $model;
    }
}

Model

models/examples/cms/HelloBlock.php

<?php
/**
 * Class HelloBlock
 *
 * {ModelResponsability}
 *
 * @package models\examples\cms
 * @category Application Model
 * @author  {AuthorName} - {AuthorEmail}
*/
namespace models\examples\cms;

use framework\Model;

class HelloBlock extends Model
{
    /**
    * Object constructor.
    *
    */
    public function __construct()
    {
        parent::__construct();
    }

    /**
    * Autorun method. Put your code here for running it after object creation.
    * @param mixed|array|null $parameters Additional parameters to manage
    *
    */
    protected function autorun($parameters = null)
    {

    }

    public function getUsers()
    {
        $users = array(
            array("Name"=>"Bob","Location"=>"England"),
            array("Name"=>"Hellen","Location"=>"USA"),
            array("Name"=>"Ciro","Location"=>"Italy")
        );

        return $users;
    }
}

View

views/examples/cms/HelloBlock.php

<?php
/**
 * Class HelloBlock
 *
 * {ViewResponsability}
 *
 * @package controllers\examples\cms
 * @category Application View
 * @author  {AuthorName} - {AuthorEmail}
*/
namespace views\examples\cms;

use framework\View;

class HelloBlock extends View
{

    /**
    * Object constructor.
    *
    * @param string|null $tplName The html template containing the static design.
    */
    public function __construct($tplName = null)
    {
        if (empty($tplName))
            $tplName = "/examples/cms/hello_block";
        parent::__construct($tplName);
    }
    
    /**
    * Sets value for WelcomeMessage placeholder
    *
    * @param mixed $value
    */
    public function setVarWelcomeMessage($value)
    {
        $this->setVar("WelcomeMessage",$value);
    }

    /**
    * Opens block Users
    *
    */
    public function openBlockUsers()
    {
        $this->openBlock("Users");
    }

    /**
    * Sets value for Location inside the block Users
    *
    * @param mixed $value
    */
    public function setBlockVarUsersLocation($value)
    {
        $this->setVar("Location",$value);
    }

    /**
    * Sets value for Name inside the block Users
    *
    * @param mixed $value
    */
    public function setBlockVarUsersName($value)
    {
        $this->setVar("Name",$value);
    }

}

HTML Template

templates/examples/cms/hello_block.html.tpl

<!DOCTYPE html>
<html>
<head>
    <title>Hello world second version</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Bootstrap core CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" media="screen">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
    <![endif]-->
</head>
<body>

<div class="container">

    <h1>{WelcomeMessage}</h1>
    <div class="well">
        This example introduces BLOCK
    </div>
    <!-- BEGIN Users -->
    <div class="row">
        <div class="col-md-6 col-xs-6">{Name}</div>
        <div class="col-md-6 col-xs-6">{Location}</div>
    </div>
	<!-- END Users -->

    <a href="https://www.webmvcframework.com/webmvc/examples/about/example/hello_block" class="btn btn-info">Show source code</a>
    <a href="https://www.webmvcframework.com/webmvc/examples/cms/hello_block/" class="btn btn-success">Template</a>
    <a href="https://www.webmvcframework.com/webmvc/examples/cms/hello_block" class="btn btn-success">Run again</a>
    <a href="https://www.webmvcframework.com/webmvc/examples/" class="btn btn-primary">Examples TOC</a>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html> 


Back