• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
include view in parser

#1
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
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, 03: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, 06: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, 02: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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.