1

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:

views/myview/view.html.php
views/myview/tmpl/default.php
views/myview/tmpl/alternate.php

And then in my class I have something like this:

class MyComponentViewMyView extends JViewLegacy {

     function display($tpl = null ) {
         if ( ViewManager::$showAlternate ) {
              $this->setLayout('alternate');
         } 
         parent::display($tpl);
     }
}

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?

2 Answers 2

1

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.

1

I have been fighting the same problem for two full days now, going over lesson 10 of Robbie Jacksons excellent video explanations in this series https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component again and again.

In lesson 10 he tries out his own alternative approach and he comes up with this redirect in his subcontroller to make the View Class based on JControllerForm use the edit.php layout:

$this->setRedirect(JROUTE::_("index.php?option=com_helloworld&view=helloworld&layout=edit&id=$id",false),$msg);

It works! It really picks up the edit.php template. However, using the same technicque I failed to make it use another template (mine was called add.php). Even though, the View class picks up the name of the layout file, it somehow ends up using edit.php anyway. Hours of debugging in VS_code with XDebug, searching API-docs etc. did not give me the solution.

In the end I ended up writing alternative subcontroller -, view- and model classes to handle add-functionality. When the view class has only one template in the tmpl-folder I managed to make it pick the right one, even if it not called default.php.

Not a fully satisfactory solution I know, but at least you know that you are not the only one out there fighting the steep learning curve of Joomla programming:-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.