CodeIgniter Forums
Extending View? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Extending View? (/showthread.php?tid=76369)



Extending View? - easyD - 05-06-2020

Hello! I'm trying to extend dashboard.php view with layout.php view. Layout.php is one level up compared to dashboard.php.

I tried

"../layout.php"
"Views/..path../layout.php"

but nothing seems to work.

Any suggestion on how to solve this issue?

[Image: view.jpg]

Edit...

I found out that this is working... "themes\default\layout". Any other solutions whit which I would avoid using the name of the theme (so in this case - theme with name "default")?


RE: Extending View? - kilishan - 05-06-2020

The way I've handled this is to create a new instance of the renderer service, giving it the path to my themes folder:

PHP Code:
$path ROOTPATH ."themes/{$this->theme}";
$renderer Services::renderer($pathnullfalse); 

Passing false as the last argument gives you a non-shared instance. Now, when you use things like $this->extend(), or $this->include() within your views, it will always find it within your theme directory. In the example I linked to, it supports multiple themes within the Themes directory.

If you need to get a view from the normal path anywhere, you can still use echo view('...'), which uses the main, shared, instance of the View library that points to the default location.


RE: Extending View? - easyD - 05-06-2020

Killishan thank you very much! It works Smile  This is the code I wrote based on your suggestion. 


PHP Code:
        $path APPPATH 'views/themes/default/'// {$this->theme}

        
$view = \Config\Services::renderer($pathnullfalse); 

        return 
$view->setVar('data'$data)->render('customer/dashboard'); 

So now I can in views files use only <?= $this->extend('layout'?> for extending the view.