Phalcon Views

View represents the front-end of the application. It consists of HTML files embedded inside PHP code which creates the view of the application. View provides the data to the web browser from your application.

Phalcon\Mvc\View and Phalcon\Mvc\View\Simple are responsible for the managing the view layer of MVC application.

Integrating Views with controller

Views are automatically integrated when controller completes its functionality. The entire view component looks inside the view folder of same file name whose last controller is executed.

Example: If a request made to url 193.168.1.1/javatpoint/phalcon/intro/911

Server Address193.168.1.1
Phalcon DirectoryJavatpoint
ControllerPhalcon
ActionIntro
Parameter911

Implementation

<?php  

use Phalcon\Mvc\Controller;  

class PostsController extends Controller  

{  

    public function indexAction()  

    {  

    }  

    public function showAction($postId)  

    {  

        // Pass the $postId parameter to the view  

        $this->view->postId = $postId;  

    }  

}

Hierarchal Rendering

It is the default component for the view rendering and located under directory Phalcon\MVC\View. Its component automatically uses PHP itself as template engine. It has extension .phtml and view component will find following 3 files.

NameFileDescription
Action Viewapp/views/posts/show.phtmlThis is the view related to the action. It only will be shown when the showaction is executed.
Controller Layoutapp/views/layouts/posts.phtmlThis is the view related to the controller. It only will be shown for every action executed within the controller “posts”. All the code implemented in the layout will be reused for all the actions in this controller.
Main Layoutapp/views/index.phtmlThis is main action it will be shown for every controller or action executed within the application.

Implementation

<!-- app/views/posts/show.phtml -->  

<h3>This is show view!</h3>  

<p>I have received the parameter <?php echo $postId; ?></p>  

  

<!-- app/views/layouts/posts.phtml -->  

<h2>This is the "posts" controller layout!</h2>  

<?php echo $this->getContent(); ?>  

  

<!-- app/views/index.phtml -->  

<html>  

<head>  

<title>Example</title>  

</head>  

<body>  

<h1>This is Phalcon Tutorial!</h1>  

<?php echo $this->getContent(); ?>  

</body>  

</html> 

Simple Rendering

It is an alternate component to Phalcon\MVC\View and located under Phalcon\MVC\View\Simple. It is similar to the MVC\View but lacks hierarchy. It allows developer to control view when it is altered and its location.

Implementation

Default component replacement

 <?php  

use Phalcon\Mvc\View\Simple as SimpleView;  

$di->set(  

    'view',  

    function () {  

        $view = new SimpleView();  

        $view->setViewsDir('../app/views/');  

        return $view;  

    },  

    true  

); 

    Now, to render we call the render() method

    <?php  
    
    use Phalcon\Mvc\Controller;  
    
    class PostsController extends Controller  
    
    {  
    
        public function indexAction()  
    
        {  
    
            // Render 'views-dir/index.phtml'  
    
            echo $this->view->render('index');  
    
            // Render 'views-dir/posts/show.phtml'  
    
            echo $this->view->render('posts/show');  
    
            // Render 'views-dir/index.phtml' passing variables  
    
            echo $this->view->render(  
    
                'index',  
    
                [  
    
                    'posts' =>Posts::find(),  
    
                ]  
    
            );  
    
            // Render 'views-dir/posts/show.phtml' passing variables  
    
            echo $this->view->render(  
    
                'posts/show',  
    
                [  
    
                    'posts' =>Posts::find(),  
    
                ]  
    
            );  
    
        }  
    
    }  
    
    Declaring simple() method  
    
    <?php  
    
    $params = [  
    
        'posts' =>Posts::find(),  
    
    ];  
    
    // Phalcon\Mvc\View  
    
    $view = new \Phalcon\Mvc\View();  
    
    echo $view->render('posts', 'show', $params);  
    
    // Phalcon\Mvc\View\Simple  
    
    $simpleView = new \Phalcon\Mvc\View\Simple();  
    
    echo $simpleView->render('posts/show', $params);  
    
    ?>

    View Events

    Phalcon\Mvc\View and Phalcon\Mvc\View\Simple are able to send events to an EventsManager if it is present.

    Event NameTriggeredBreak Operation
    beforeRenderTriggered before starting the render processYes
    beforeRenderViewTriggered before rendering an existing viewYes
    afterRenderViewTriggered after rendering an existing viewNo
    afterRenderTriggered after completing the render processNo
    notFoundViewTriggered when a view was not foundNo

    Implementation

     <?php  
    
    use Phalcon\Events\Event;  
    
    use Phalcon\Events\Manager as EventsManager;  
    
    use Phalcon\Mvc\View;  
    
    $di->set(  
    
        'view',  
    
        function () {  
    
            // Create an events manager  
    
            $eventsManager = new EventsManager();  
    
            // Attach a listener for type 'view'  
    
            $eventsManager->attach(  
    
                'view',  
    
                function (Event $event, $view) {  
    
                    echo $event->getType(), ' - ', $view->getActiveRenderPath(), PHP_EOL;  
    
                }  
    
            );  
    
            $view = new View();  
    
      
    
            $view->setViewsDir('../app/views/');  
    
            // Bind the eventsManager to the view component  
    
            $view->setEventsManager($eventsManager);  
    
            return $view;  
    
        },  
    
        true  
    
    ); 

      Comments

      Leave a Reply

      Your email address will not be published. Required fields are marked *