Documentation

View

Views is where the HTML of your application goes. A view can be an entire web page or just a small section of a page like a header, sidebar, navigation or footer. Using views allows you to separate the presentation and business logic of your application.

View Manager

This is our View Manager configuration in module.config.php. For us to get a better understanding lets have a look on our View Manager configuration.

    
        return [
            'view_manager' => [
                'template_map' => [
                    // Location of the layout template
                    'layout/layout' => 'layout/layout.phtml',
                    // Location of the 404 template
                    'error/404' => 'error/404.phtml',
                ],
                // Where all the view files or templates are located
                'template_path_stack' => __DIR__ . '/../view',
            ],
        ];
    
                

Creating a Template

Please note that you have to follow the right structure on creating a template for View to render it properly.

Let's have an example below, we have a module Application and a controller of AboutController.php with a method of authorAction()

                    
    application_root/
        module/
            Application/ // Module
                src/
                    Controller/
                        AboutController.php // Controller
                view/
                    
                
    
    namespace namespace Application\Controller;

    use JiNexus\Mvc\Controller\AbstractController;
    use JiNexus\Mvc\Model\ViewModel;

    /**
     * Class AboutController
     * @package Application\Controller
     */
    class AboutController extends AbstractController
    {
        /**
         * @return ViewModel
         */
        public function authorAction()
        {
            return new ViewModel();
        }
    }
    
                

The above example will have a view structure like this:

                    
    application_root/
        module/
            Application/ // Module
                src/
                view/
                    application/ // Module
                        about // Controller
                            author.phtml // Method
                    
                

Passing variables from Controller to the View

There are three ways to pass variables from Controller to Views.

First is using ViewModel()

    
    namespace namespace Application\Controller;

    use JiNexus\Mvc\Controller\AbstractController;
    use JiNexus\Mvc\Model\ViewModel;

    /**
     * Class IndexController
     * @package Application\Controller
     */
    class IndexController extends AbstractController
    {
        /**
         * @return ViewModel
         */
        public function indexAction()
        {
            // Pass a variable to the view
            return new ViewModel([
                'foo' => 'Hello',
                'bar' => 'World!',
            ]);
        }
    }
    
                

Second is using set() method from View

    
    namespace namespace Application\Controller;

    use JiNexus\Mvc\Controller\AbstractController;
    use JiNexus\Mvc\Model\ViewModel;

    /**
     * Class IndexController
     * @package Application\Controller
     */
    class IndexController extends AbstractController
    {
        /**
         * @return ViewModel
         */
        public function indexAction()
        {
            // Pass a variable to the view
            $this->view->set('foo', 'Hello');
            $this->view->set('bar', 'World!');
        }
    }
    
                

And third is using setVariables() method from View

    
    namespace namespace Application\Controller;

    use JiNexus\Mvc\Controller\AbstractController;
    use JiNexus\Mvc\Model\ViewModel;

    /**
     * Class IndexController
     * @package Application\Controller
     */
    class IndexController extends AbstractController
    {
        /**
         * @return ViewModel
         */
        public function indexAction()
        {
            // Pass a variable to the view
            $this->view->setVariables([
                'foo' => 'Hello',
                'bar' => 'World!',
            ]);
        }
    }
    
                

Accessing variables from Controller to the View

Accessing variables is very straight forward, see the example below:

    
        <div>
            <p><?php echo $this->foo . ' ' . $this->bar; ?></p>
        </div>
    
                

The above example will output:

    
        Hello World!
    
                

Available View Methods

These are the list of available view methods that can be use in templates.

Method Description
basePath() Get the base path of the application
baseUrl() Get the base URL of the application
content() Render content of the Module, Controller and Method from the match route
htmlEncode() Recursively make a value safe for HTML
htmlDecode() Recursively decode an HTML encoded value
get() Get a view variable
set() Set a view variable
setVariables() Set all variables
render() Render a file
url() Get the URL from routes using a routeName