Posts: 21
Threads: 1
Joined: Jan 2019
Reputation:
0
01-01-2019, 11:27 PM
(This post was last modified: 01-08-2019, 08:39 PM by adnzaki.)
Hi, I'm so excited with brand new template parser in CI4. But I got a problem when including file directly in template, because in CI3 I used to use this simple code to include view inside a view
In CI4's parser, (as we know) we cannot write any PHP code inside a template, so I added a plugin to include another view.
The question, is there a way in parser for including view directly? If not, it would be great if CI4's parser comes with functionality to include file directly (nested view).
And here is plugin I added to parser:
PHP Code: public static function include(array $params = []) { $parser = \Config\Services::parser(); return $parser->render(...$params); }
// register plugin in Config/View.php public $plugins = [ 'include' => '\Actudent\Core\Config\ActudentParserPlugin::include', ];
// call it in view {+ include Actudent\Core\Views\component\head +}
Thanks CI4 team!
Posts: 365
Threads: 5
Joined: May 2015
Reputation:
32
I have to admit I have not personally played with v4 yet, but I think this might be what you are after:
https://codeigniter4.github.io/CodeIgnit...=view#view
PHP Code: <?= view('viewfile', ...) ?>
Posts: 58
Threads: 10
Joined: May 2017
Reputation:
4
01-02-2019, 05:09 AM
(This post was last modified: 01-02-2019, 05:10 AM by puschie.)
you could also use your view as variable replacement
PHP Code: $template = "Hello, {var1} {firstName} {lastName}"; $data = [ 'var1' => view( VIEW_NAME ), 'firstName' => 'max', 'lastName' => 'mustermann' ]; echo $renderer->setData( $data )->renderString( $template );
makes it much easier to see dependencies and no view nesting.
Posts: 21
Threads: 1
Joined: Jan 2019
Reputation:
0
(01-02-2019, 02:00 AM)Pertti Wrote: I have to admit I have not personally played with v4 yet, but I think this might be what you are after:
https://codeigniter4.github.io/CodeIgnit...=view#view
PHP Code: <?= view('viewfile', ...) ?>
Unfortunately, we couldn't place any PHP code inside the template. that's why I created additional plugin for including template inside a template
Posts: 21
Threads: 1
Joined: Jan 2019
Reputation:
0
(01-02-2019, 05:09 AM)puschie Wrote: you could also use your view as variable replacement
PHP Code: $template = "Hello, {var1} {firstName} {lastName}"; $data = [ 'var1' => view( VIEW_NAME ), 'firstName' => 'max', 'lastName' => 'mustermann' ]; echo $renderer->setData( $data )->renderString( $template );
makes it much easier to see dependencies and no view nesting.
I had tried that way but the parser could't load the view as expected. The other way, I added 'raw' in the second argument
Code: setData($data, 'raw')
but parser cannot load the data from view('view', $data).
Posts: 365
Threads: 5
Joined: May 2015
Reputation:
32
Ah, so you are using new templating engine? https://codeigniter4.github.io/CodeIgnit...arser.html
I guess it depends on what kind of PHP code you want to put in your view file, but it probably shouldn't do anything beyond IF checks, which you can do in templating engine:
Code: {if $role=='admin'}
<h1>Welcome, Admin</h1>
{elseif $role=='moderator'}
<h1>Welcome, Moderator</h1>
{else}
<h1>Welcome, User</h1>
{endif}
You should do everything else on Controller or Model side, so view itself shouldn't try to go and figure things out, it should be just - here's data to render, here#s maybe slightly different layout or styling if some conditions are met.
Posts: 21
Threads: 1
Joined: Jan 2019
Reputation:
0
(01-09-2019, 01:34 AM)Pertti Wrote: Ah, so you are using new templating engine? https://codeigniter4.github.io/CodeIgnit...arser.html
I guess it depends on what kind of PHP code you want to put in your view file, but it probably shouldn't do anything beyond IF checks, which you can do in templating engine:
Code: {if $role=='admin'}
<h1>Welcome, Admin</h1>
{elseif $role=='moderator'}
<h1>Welcome, Moderator</h1>
{else}
<h1>Welcome, User</h1>
{endif}
You should do everything else on Controller or Model side, so view itself shouldn't try to go and figure things out, it should be just - here's data to render, here#s maybe slightly different layout or styling if some conditions are met. Ok thanks, but I guess I will use my own plugin to include template inside a template. It replaced old code like
PHP Code: <?php $this->view('viewfile) ?> with {+ include Path\To\template +}
And it works well
Posts: 1,285
Threads: 65
Joined: Oct 2014
Reputation:
82
Good call. Can't believe we overlooked that one! I'll add it to the to do list.
Posts: 21
Threads: 1
Joined: Jan 2019
Reputation:
0
04-20-2020, 09:57 PM
(This post was last modified: 04-20-2020, 09:58 PM by adnzaki.)
(04-23-2019, 06:39 AM)kilishan Wrote: Good call. Can't believe we overlooked that one! I'll add it to the to do list. Whoops, I missed my own thread for a long time. Actually I got that idea from CodeIgniter4 official user guide, and implemented in my own project. I have tried that additional plugin in HMVC style (like I'm doing in my project) and it works very well.
|