How to change load layout in Joomla view?
Asked Answered
B

3

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.

Barney answered 14/11, 2012 at 13:14 Comment(3)
I think it's layout=lol.Edward
could you please paste the full url that you are passing?Edward
@Edward localhost ))) But problem already solvedBarney
B
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);
Barney answered 14/11, 2012 at 13:42 Comment(3)
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?Tyson
oh I did $view= new JView(); $view->setLayout('dafault:test'); and it worked :)Tyson
This code will cause joomla to ignore the layout request parameter. Please see my answer: https://mcmap.net/q/1066901/-how-to-change-load-layout-in-joomla-viewLoeffler
L
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);
      }

    # ...
Loeffler answered 5/8, 2015 at 13:9 Comment(0)
P
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.

Passion answered 5/11, 2015 at 6:55 Comment(1)
Thanks, Totally overlooked the $tpl variable.Devilry

© 2022 - 2024 — McMap. All rights reserved.