Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change load layout in Joomla view?

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.

like image 486
user1692333 Avatar asked Nov 14 '12 13:11

user1692333


3 Answers

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);
like image 58
user1692333 Avatar answered Oct 06 '22 00:10

user1692333


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);
      }

    # ...
like image 24
Ozzy Avatar answered Oct 05 '22 23:10

Ozzy


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.

like image 30
Seoras Avatar answered Oct 05 '22 23:10

Seoras