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.
Important Note!
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.
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()
The above example will have a view structure like this:
Important Note!
All views must be located in the module/{Module}/view directory. You can of course create subdirectories to better organize your view files.
Passing variables from Controller to the View
There are three ways to pass variables from Controller to Views.
First is using ViewModel()
Second is using set() method from View
And third is using setVariables() method from View
Accessing variables from Controller to the View
Accessing variables is very straight forward, see the example below:
The above example will output:
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
If you found a typo or error, please help us improve this document.
Contact Us