Welcome Guest, Not a member yet? Register   Sign In
Add change view folder for VIew
#1

(This post was last modified: 12-30-2023, 12:58 AM by ozornick.)

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'); 
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#2

(This post was last modified: 12-30-2023, 01:29 AM by kenjis.)

Why do you need to change the view path dynamically?

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

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
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#4

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

Reply
#5

(This post was last modified: 12-30-2023, 11:51 PM by ozornick.)

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
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#6

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

I know. It's not working right now. Somewhere initialization happens earlier. Therefore, changing the folder on the fly would be a good solution.
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#8

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

Do you think this is right? If you need to change it again later in the code?
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#10

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?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB