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.
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',
],
];
Your views must use the .phtml extension for them to get rendered by the template renderer. This is just a formality used to denote a file that is more HTML dense rather than PHP.
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
All views must be located in the module/{Module}/view directory. You can of course create subdirectories to better organize your view files.
There are three ways to pass variables from Controller to Views.
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!',
]);
}
}
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!');
}
}
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 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!
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 |
If you found a typo or error, please help us improve this document. Contact Us