How can I show an alternate layout for my view in an MVC component?

Jaxon: 6 days ago

I am trying to choose the layout for my view dynamically, which is what I imagined setLayout or loadTemplate on my JViewLegacy view would be able to do, but there seems to be some extra convention going on here that I can't seem to follow.

If my structure looks like this:


And then in my class I have something like this:

class MyComponentViewMyView extends JViewLegacy {

     function display($tpl = null ) {
         if ( ViewManager::$showAlternate ) {

What I would expect from the documentation I have been able to find is that this would load the view from 'alternate.php' instead of 'default.php' but instead I get a message like this: 500 Layout default_alternate not found.

Things that I have found recommended and tried that don't seem to have made a difference, or the only difference they have made was a slight change of error message, usually to Layout default not found:

  • Changing from setLayout to loadTemplate.
  • Just using the layout parameter in the URL to test if it can be found.
  • Renaming alternate.php to default_alternate.php or even alternate_default.php
  • Changing the path to /view/myview/alternate/default.php instead of /view/myview/tmpl/default.php
  • Calling setLayout with default:alternate.php instead.

There is clearly something I have missed or not been able to find in the documentation. What do I need to do to be able to render alternate.php instead of default.php for the same view?

Serenity: 6 days ago

The approach given above seemed like it should work because it does. However, if your class is a little more complex than my example and you have a __construct() function don't forget to call parent::__construct() or it won't initialise the list of paths to search for your custom views and you will end up with this error message.