Skip to content
Nicholas K. Dionysopoulos edited this page May 2, 2017 · 2 revisions

The View is that part of MVC which actually renders the data into a suitable representation. The "suitable representation" is usually HTML but it can really be anything: plain text, JSON, XML, CSV, RSS/Atom Feed, an image, a video, anything really. It's up to you to decide what are your component's needs and define what a "suitable representation" means in this context.

View vs DataViewInterface

Unlike FOF 1.x and 2.x, FOF now comes with different View classes. The simple FOF30\View\View class is used for views which do not talk directly to a database table. For example it could be your component's configuration, a file manager view etc. The view classes under the FOF30\View\DataView namespace, implementing the DataViewInterface interface are used for data-aware views which talk directly to a database table (more precisely: to a DataModel). This separation makes it easier to handle non-data views.

This page talks about the plain View. You may want to also read the documentation pages on each DataView by following the links in the sidebar.

Class and file naming conventions

The view class files are found inside your component's View directory. The view must be named after the format it supports, with the first letter capitalised. For example "Html" for a view which is supposed to handle HTML output, i.e. when the URL parameter format=html is present in the request. Likewise, the view class should be named Xml if it expect to handle format=xml. You get the idea. Do note that Joomla! and FOF assume format=html if the format parameter is not present in the request.

The View class' name is the same as the filename and the namespace depends on what is the namespace of your component, which view we're in and which section (frontend, backend) you're talking about.

Considering a component com_example with namespace prefix Acme\Example, a backend view Items and format=html, the file administrator/components/com_example/View/Items/Html.php has the following contents:

<?php

namespace Acme\Example\Admin\View\Item;

class Html extends FOF30\View\View
{
    // View implementation goes here
}

View template files and their location

Location on disk

The view template files of a view can be found inside subdirectories of the component's ViewTemplates directory. For example, if you have a component com_example and a back-end view Items the view templates for this view will be sought for in the directory administrator/components/com_example/ViewTemplates/Items.

If a view template is not found there, FOF will look in the legacy directories, i.e. the tmpl directory of each view. For example, if you have a component com_example and a back-end view Items the view templates for this view will be sought for in the directory administrator/components/com_example/View/Items/tmpl.

We recommend using the ViewTemplates directory to separate your presentation from your business logic. Further to that, the ViewTemplates directory has exactly the same structure as Joomla's template overrides, making it easier for frontend developers to understand the correlation between template override and original file.

File naming

The name of the view template being searched for depends on whether you are using a PHP (classic), Blade or XML Form view template. In all cases the requested layout will be taken into account. Assuming they current layout is called something, FOF will look for these view templates, in this order:

  • something.blade.php (Blade template)
  • something.php (classic PHP template)
  • form.something.xml (XML form template)

If no layout is provided, the layout name default will be used.

If, however, you're using a DataController and you have not provided a layout name, the name of the layout will be different depending on the task name:

Task Layout Effective template names
browse or any other default default.blade.php, default.php, form.default.xml
edit, add form form.blade.php, form.php, form.form.xml
read item item.blade.php, item.php, form.item.xml

Please read the factory documentation to learn more about where view templates will be sought for. Some factories will look automatically in both plural and singular named views and/or in the back-end and front-end.

Joomla! version specific overrides

It is possible have different view templates per Joomla! version or version family. The correct view template is chosen automatically, without you writing a single line of code.

Let's say that you have a view using the default.php view template file. Let's say that you want your component to work on Joomla! 3.x and 4.x (whenever it's released...). The markup is most likely different for each Joomla! version, Javascript has changed, different features are available… Well, no problem! FOF will automatically search for view template files (including XML forms) suffixed with the Joomla! version family or version number.

For example, if you're running under Joomla! 3.3, FOF will look for default.j33.php, default.j3.php and default.php in this order. If you're running under Joomla! 4.0, FOF will look for default.j40.php, default.j4.php and default.php in this order. This allows you to have a different view template file for each version family of Joomla! without unaesthetic if-blocks and awkward code.

This feature also works with Blade templates and XML forms. The Joomla! version prefix is added before the file extension, e.g. default.j33.blade.php or form.default.j33.xml.

Template overrides

FOF supports the same template override feature as Joomla! itself.

For example, to override the view template file administrator/components/com_example/View/Items/tmpl/default.php copy it to administrator/templates/YOUR_TEMPLATE/html/com_example/Items/default.php and modify the copied file. FOF will look for template overrides before looking for the view template file in the component's directory.

IMPORTANT Overriding one type of view template file with another is not supported and may not work as expected. For example, overriding administrator/components/com_example/View/Items/tmpl/form.default.xml with administrator/templates/YOUR_TEMPLATE/html/com_example/Items/default.blade.php is not supported and we can't guarantee it will work reliably. Always override an XML form with an XML form, a Blade template with a Blade template and a classic PHP template with a classic PHP template.

Customising your View

You should only need to create new methods to handle the events fired before and after displaying the view. The events raised are onBeforeTaskname and onAfterTaskname where taskname is the name of the Controler task currently being rendered.

For example, if you want to handle the event which runs before rendering the view for the foobar task you need to define

public function onBeforeFoobar()
{
    // Do something here...
    
    return true;
}

It is advisable to not override the display() method.

These events can also be handled by Joomla! plugins. Please read the page on using Joomla! plugins for FOF events.

Clone this wiki locally