10

By default parent::display($tpl); loads components/com_my_component/views/my_component/tmpl/default.php, but in some cases i need to load other php file which is in the same folder near default.php (for example components/com_my_component/views/my_component/tmpl/lol.php). How to do this from view.html.php.

P.S.

Tried load loadTemplate and setLayout methods with no luck.

user1692333
  • 2,461
  • 5
  • 32
  • 64

3 Answers3

12

Solved the problem by myself. Need to use the method setLayout and pay attention to the input syntax

$this->setLayout('dafault:lol');
parent::display($tpl);
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
user1692333
  • 2,461
  • 5
  • 32
  • 64
  • I get an error in the controller Fatal error: Call to undefined method FcseController::setLayout() in /var/www/pygmaxia2.gr/components/com_fcse/controller.php on line 33 and the code in the line is this $this->setLayout('dafault:test'); Any Idea? – themhz Apr 08 '13 at 13:40
  • oh I did $view= new JView(); $view->setLayout('dafault:test'); and it worked :) – themhz Apr 08 '13 at 13:43
  • This code will cause joomla to ignore the `layout` request parameter. Please see my answer: http://stackoverflow.com/a/31833510/1031312 – Ozzy Aug 05 '15 at 13:10
3

By default, joomla looks for the layout keyword in the URL to decide which layout to display. If this variable is empty or not present then the tmpl/default.php layout will be loaded.

By editting your view.html.php file you can set the default layout by using the JView API, e.g. $this->setLayout('lol') will make the URL example.com/yourview equivalent to example.com/yourview?layout=lol.

However, this change alone will result in Joomla overriding it's default behaviour so that the layout request will be ignored. This means that the request example.com/yourview?layout=lmao will also display example.com/yourview = example.com/yourview?layout=lol

You can solve this easily by adding a condition around the setLayout function so that only if the layout keyword is not present then you will set the default layout to lol, e.g.

    <?php 
    # ...

      function display($tpl = null) {
        # ...

        # Edit : Set the default layout to 'lol'
        $layout = JRequest::getWord('layout', '');
        if (empty($layout)) $this->setLayout("lol");

        // Display the view
        parent::display($tpl);
      }

    # ...
Ozzy
  • 8,244
  • 7
  • 55
  • 95
2

I keep coming back to this and I've yet to find a satisfying solution.

What does work, from J1.5 right up to J3.4, for me has always been to set the $tpl variable in view.html.php

If $tpl is empty or "" then tmpl/default.php is displayed by default.

If you change $tpl to a string, e.g. $tpl="stacker" then it will look for and display tmpl/default_stacker.php

I've seen various differing theories on changing it earlier in the MVC so that it doesn't need the default_ pretext. e.g. tmpl/stacker.php None have worked for me.

Seoras
  • 1,286
  • 13
  • 21