Welcome Guest, Not a member yet? Register   Sign In
include view in parser
#1

(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
Code:
$this->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!
Reply
#2

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', ...) ?>
Reply
#3

(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' => viewVIEW_NAME ), 'firstName' => 'max''lastName' => 'mustermann' ];
echo 
$renderer->setData$data )->renderString$template ); 

makes it much easier to see dependencies and no view nesting.
Reply
#4

(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
Reply
#5

(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' => viewVIEW_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).
Reply
#6

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.
Reply
#7

(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 Smile
Reply
#8

Good call. Can't believe we overlooked that one! I'll add it to the to do list.
Reply
#9

(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.

[Image: iHLHOFZhpycxSmBm2qJdSbYDkOqj-_uzTjrwG0_j...w1366-h219]
Reply




Theme © iAndrew 2016 - Forum software by © MyBB