-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: allow overriding namespaced views via app/Views directory
#9860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.7
Are you sure you want to change the base?
feat: allow overriding namespaced views via app/Views directory
#9860
Conversation
|
It looks good for modules. Questions:
|
CodeIgniter will look for: app/Views/Example/Blog/Views/blog_view.php. If the user explicitly includes
Yes, if a file exists in the matching path within app/Views, it will take precedence over the module's file. |
michalsn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether we should add a configuration option for this, allowing users to disable the functionality. The chances of this breaking existing code are small, though.
As for the difference between view('Example\Blog\blog_view') and view('Example\Blog\Views\blog_view'), this is a module developer's decision. We should not attempt to manipulate or normalize it, as that could lead to issues when developers choose more complex or non-standard folder structures.
Co-authored-by: Michal Sniatala <[email protected]>
We could technically add a flag to app\Config\View.php to toggle this behavior. However, I personally believe it is unnecessary. Since this logic only triggers when a user deliberately creates a matching directory structure in app/Views corresponding to a specific namespace, the probability of an accidental collision is extremely low.
Exactly. The current implementation intentionally avoids any magic stripping of segments like Views. It performs a direct 1-to-1 mapping of the namespace string to the directory path. This ensures predictable behavior regardless of how the module developer structured their views. |
Co-authored-by: Michal Sniatala <[email protected]>
Co-authored-by: Michal Sniatala <[email protected]>
| $this->renderVars['file'] = $this->viewPath . $this->renderVars['view']; | ||
|
|
||
| if (str_contains($this->renderVars['view'], '\\')) { | ||
| $this->renderVars['file'] = $this->viewPath . ltrim(str_replace('\\', DIRECTORY_SEPARATOR, $this->renderVars['view']), DIRECTORY_SEPARATOR); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not accurate, we can add a directory to app/Views: _modules, share,.. to explicitly separate the overwritable templates. As app/Views/share/Example/Blog/blog_view.php.
There is unlikely to be an NS with such a shared\Example\Blog value, so it looks safer. If desired, this folder can be customized. The auto-selection of the search should remain.
|
I've been trying modularity for a long time and working with templates is causing problems. I would not like to develop templates for Bootstrap when the global project theme is written in TailwindCSS. This feature seems to solve this problem - I don't need to create templates for the module. |
Description
This PR adds the ability to override namespaced views (typically from modules or packages) by placing a matching file structure within the application's local app/Views directory.
See : https://forum.codeigniter.com/showthread.php?tid=93684&pid=427797#pid427797
Checklist: