CodeIgniter Forums
Add change view folder for VIew - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Add change view folder for VIew (/showthread.php?tid=89062)

Pages: 1 2


Add change view folder for VIew - ozornick - 12-30-2023

I would like to add a method to change the View->setViewPath($path) template catalog.
Because you can't change the folder for the shared class: Services::renderer(...)
The modified object cannot be returned in the filter. Is this function needed in the framework?

PHP Code:
// Before (default) $theme = 'standart'
// $viewPath = 'Views/'
// $file = 'Views/standart/user/dashboard.php'
return view($theme '/user/dashboard');

// After setViewPath('Views/standart')

// $viewPath = 'Views/standart'
// $file = 'Views/standart/user/dashboard.php'
return view('/user/dashboard'); 



RE: Add change view folder for VIew - kenjis - 12-30-2023

Why do you need to change the view path dynamically?

You can set the path if you call Services::renderer($viewPath).


RE: Add change view folder for VIew - ozornick - 12-30-2023

As standard, change the theme folder. The theme is stored in the database. 
1. The change in BaseController is not global.
2. It is unwise to create each View anew.
I consider it normal to change in the process


RE: Add change view folder for VIew - kenjis - 12-30-2023

If you don't need to change the path in a request more than once,
the following code works.


PHP Code:
<?php

namespace App\Controllers;

use 
Config\Services;

class 
Home extends BaseController
{
    public function __construct()
    {
        $viewPath APPPATH 'Views/standart';
        Services::renderer($viewPath);
    }

    public function index(): string
    
{
        // 'Views/standart/foo.php'
        return view('foo');
    }




RE: Add change view folder for VIew - ozornick - 12-30-2023

I haven't checked it yet, but what happens when this instance is created earlier? For example, after view(), view_cell(), ..?
 Or do you need to have a template engine in the services: User Manager, Sender, Auth...?
Initialization will be called earlier and may not be the one that is needed.

UPD: This method is not suitable for calling in filters. As well as the creation in the constructor. The folder does not change, I probably have initialization of services somewhere before


RE: Add change view folder for VIew - kenjis - 12-31-2023

You must call Services::renderer($viewPath) before calling view() in anywhere.
The first instance created by Services is shared.


RE: Add change view folder for VIew - ozornick - 12-31-2023

I know. It's not working right now. Somewhere initialization happens earlier. Therefore, changing the folder on the fly would be a good solution.


RE: Add change view folder for VIew - kenjis - 12-31-2023

If you want to use it in filters, why don't you call Services::renderer($viewPath) in pre_system Event?


RE: Add change view folder for VIew - ozornick - 12-31-2023

Do you think this is right? If you need to change it again later in the code?


RE: Add change view folder for VIew - kenjis - 02-12-2024

I don't see why you need to change the path dynamically.
Does the theme change in one request?
Why don't you set the theme's view path at first?