Work In Progress

This book is currently work in progress. Some sections are not yet written. Thank you for your understanding!

Views

Views, in Joomla 3, referred to two different things: the View classes and the view templates. In fact, the view templates were placed in a tmpl folder under each View's folder which was a bad idea: you had the output rendering in a subdirectory of the business logic, making it hard to discern when one ends and the other begins.

In Joomla 4 there is a separation of these two very different types of code. The view classes (which we will call Views) are placed in the src/View folder with one subdirectory for each view in Uppercasefirst format. The view templates are placed in the tmpl folder in your component's folder with one subdirectory for each view they correspond to in lowercase format.

[Warning]Warning

Folder and file name case matters. You may not notice it when developing on a Windows or macOS machine with a case-insensitive filesystem but you will definitely suffer if you mix it up when deploying your component on a Linux-based server where the filesystem is virtually guaranteed to be case-sensitive.

Let's say you have a view which is called Foobar.

Your HTML View class file path relative to your component's root MUST be src/View/Foobar/HtmlView.php. It can NOT be src/View/FooBar/HtmlView.php (mixed case in the subdirectory) or src/View/Foobar/HTMLVIEW.php (wrong case for the filename)!

Your view default.php view template file path relative to your component's root MUST be tmpl/foobar/default.php. It cannot be tmpl/Foobar/default.php (wrong case for the subdirectory)!

Knowing this will save you hours of “fun” hitting your head against a brick wall, wondering why it works on your computer but not on your server.

The bulk of the implementation logic for Joomla 4 MVC Views is the same as in Joomla 3 MVC.

The View classes extend from one of the base Joomla MVC View super-classes:

\Joomla\CMS\MVC\View\AbstractView

This is the base class all views extend from. You will have to implement the display method yourself.

It is very rare that you are going to extend from this view. Typically, this is something you will do when you are rendering a non-HTML document, without using a view template file. Do note that if you are outputting JSON you may want to use \Joomla\CMS\MVC\View\JsonView instead.

\Joomla\CMS\MVC\View\HtmlView

This is the most common view type, used whenever you want to display HTML output in the front- or backend.

[Note]Note

Even though Joomla defines the \Joomla\CMS\MVC\View\ListView class for administrator list views and \Joomla\CMS\MVC\View\FormView for add/edit views, these view classes are no longer used in Joomla 4. Both types of pages use the HtmlView instead. As a result, the two more specialised view types will not be mentioned again in this book.

\Joomla\CMS\MVC\View\JsonView

This view type is used to output a JSON document. You set its _output property and that's all there is to it. This is rarely used on its own.

\Joomla\CMS\MVC\View\JsonApiView

This view type is used exclusively in the Api (JSON API) application. It's used to output data from the JSON API in JSON format (what else?).

\Joomla\CMS\MVC\View\CategoriesView

This view type is used in the frontend (site) application to display a list of categories. You are supposed to use this if you are using Joomla's core categories in your component.

\Joomla\CMS\MVC\View\CategoryView

This view type is used in the frontend (site) application to display a single category, typically with a list of its subcategories and items. You are supposed to use this if you are using Joomla's core categories in your component.

\Joomla\CMS\MVC\View\CategoryFeedView

This view type is used in the frontend (site) application to display an RSS / Atom feed of a category's objects. You are supposed to use this if you are using Joomla's core categories in your component.

Since Joomla 4.2.0 you can use $this->getCurrentUser() in a view to get the Joomla User object for the currently logged in user.

Since Joomla 4.0.0 you can get the Joomla event dispatcher with $this->getDispatcher() to run plugin events as explained in the Controller documentation.