Skip to content
This repository was archived by the owner on Aug 31, 2025. It is now read-only.

The Layout package

Nicholas K. Dionysopoulos edited this page Apr 7, 2015 · 1 revision

JLayout helper

Joomla! 3.2 introduced a new concept for outputting reusable, overrideable, common blocks of HTML called JLayout. FOF 3 includes a helper class which allows you to render JLayouts easily.

The FOF way, from PHP templates:

FOF30\Layout\LayoutHelper::render($this->container, 'joomla.content.helloworld', [
    'name' => 'Bob'
]);

This substitutes Ye Olde Joomla! Way of doing:

$layout = new JLayoutFile('joomla.content.helloworld');
$data = array('name' => 'Bob');
echo $layout->render($data);

On top of that, the LayoutHelper class will make sure that you're using FOF's conventions of template overrides to get layout files. In simple terms, if you're calling it from the back-end it will try to load layouts from administrator/layouts. Otherwise it will try to load layouts from the layouts directory. If you have a template override of a layout then the override will be loaded.

Should I use layouts instead of view templates?

Layouts are supposed to complement view templates, not replace them. They are only useful if you are dealing with blocks of output which should be reusable from outside your own component. If you have a common block which would be used from inside your component only you are strongly advised to use a regular PHP or Blade view template and use loadAnyTemplate to render it anywhere you want.

If you are providing a common block which should be used by extensions other than your component, without having to go through your Controller and View (HMVC) you should consider providing a JLayout instead. Keep in mind that this comes with some pitfalls:

  • You have to install your files under the layouts directory of the site manually, using the file package type.
  • You will have to educate your users that if they want to override the output of your component they will need to look in two places, components/com_something/View/something/tmpl and layouts/com_something. This is confusing to site integrators, hence our advice to avoid doing this unless strictly necessary.

There is, however, a good reason to use Joomla!'s own layouts in your components. These layouts will be overridden by template developers to match their templates' styling. This is already used by Joomla! itself to render things like editors, modal dialogs, the tollbar, quick icons and so on. If you're using JHtml to render something it already uses JLayout under the hood. If you do not wish to use JHtml it's a good idea going through JLayout with the LayoutHelper to get properly styled output.

Note: Apparently the JLayout system in Joomla! 3.2 and later is a port of (some of) the loadAnyTemplate functionality of FOF into the Joomla! core by a FOF and Joomla! contributor. We consider FOF's loadAnyTemplate functionality to be more flexible since it supports not only plain old PHP view templates but also Blade view templates and XML forms. Moreover, the loadAnyTemplate feature lets you specify more easily where to load each template and follows FOF's magic Factory conventions when the Magic or MagicSwitch Factory is in use, something that doesn't happen with JLayout.

Clone this wiki locally