CodeIgniter Forums
How to use LayoutView with parser? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to use LayoutView with parser? (/showthread.php?tid=77311)

Pages: 1 2


How to use LayoutView with parser? - aleckyann - 08-14-2020

Hi guys Smile

I looked everywhere but I still haven't found ways to use the layout system when I render my pages through Parser ...

The pages created by the parser simply ignore the php codes responsible for using ViewLayout ...

Example:
Code:
echo $this->parser->setData($data)->render('website/pages/indexPage');

not work:
Code:
<?php $this->renderSection('css') ?>

not work:
Code:
{ $this->renderSection('css') }

Does anyone know how I can use them together?  Huh


RE: How to use LayoutView with parser? - InsiteFX - 08-14-2020

I do not think it will work because they are two different entities.


RE: How to use LayoutView with parser? - aleckyann - 08-14-2020

(08-14-2020, 11:49 AM)InsiteFX Wrote: I do not think it will work because they are two different entities.

Confused  It makes no sense...

It's as if we couldn't use the layout system with Blade in Laravel ...

I still hope that this is possible ... Sad


RE: How to use LayoutView with parser? - falselight - 08-16-2020

This really important feature. I'll want use LayoutView with parser cuz uncomfortable use native php code.


RE: How to use LayoutView with parser? - InsiteFX - 08-16-2020

The View Parser:

Using the Parser, in your view templates are processed only by the Parser itself, and not like a conventional view PHP script. PHP code in such a script is ignored by the parser, and only substitutions are performed.

This is purposeful: view files with no PHP.

So maybe it would work if you ran the Parser on the Layout using:


PHP Code:
$parser = \Config\Services::parser();

$newView $parser->setData($data)
             ->render('layout');

echo 
view($newView); 

I do not have the time to play with this but you may like to give it a try.


RE: How to use LayoutView with parser? - aleckyann - 08-21-2020

(08-16-2020, 08:17 AM)InsiteFX Wrote: The View Parser:

Using the Parser, in your view templates are processed only by the Parser itself, and not like a conventional view PHP script. PHP code in such a script is ignored by the parser, and only substitutions are performed.

This is purposeful: view files with no PHP.

So maybe it would work if you ran the Parser on the Layout using:


PHP Code:
$parser = \Config\Services::parser();

$newView $parser->setData($data)
             ->render('layout');

echo 
view($newView); 

I do not have the time to play with this but you may like to give it a try.

Unfortunately it doesn't. And this impossibility is really demotivating ...
It seems very obvious that both features can work together as it is in Laravel ...


RE: How to use LayoutView with parser? - glarson - 04-16-2021

It should work.

To do this, you will have one PHP file containing the layout (the 'layout' file), another one that is supplied as an argument to the call to view() (the 'view' file), and then a third one that is supplied as an argument to the call to the parser's render() function (the 'parser' file).

You first obtain the string returned from the call to the parser->render(). This will be plain html, but should be missing whatever the layout provides. You need to create your parser file with this in mind.  You don't immediately echo this string out, but instead provide it as input data to your call to the view() function.  You can put this string into a single variable, and then echo it out inside of the view file.

So your view file will look something like this:
Code:
<?= $this->extend('\layouts\my_layout_file') ?>

<?= $this->section('content') ?>
<?= $body ?>
<?= $this->endSection() ?>

And then your controller is going to have a sequence something like:
$parser = \Config\Services::parser();
$parser_data = ['parser_data' => [...]];
$parser_text = $parser->setData($parser_data)->render('parser_file');
return view('view_file', ['body'=>$parser_text]);

within your view_file, you will just put <?= $body ?> within the layout section.


RE: How to use LayoutView with parser? - Ilham Fadil - 04-30-2021

It will works if you add comments to the view

create parser and return it inside a view

Code:
$parser = Services::parser();
$render = $parser->setData($data)->render('layout_files');

return view('layout_files', array_merge(
    ['content' => $render],
    $data
));


and for the view like this

add html layout below the comment
Code:
{#
<?= $this->extend('template/app') ?>
<?= $this->section('content') ?>
<?= $content ?>
<?= $this->endSection() ?>
#}

<html></html>



RE: How to use LayoutView with parser? - titounnes - 02-10-2022

i'm using this trick.

$view = view ('partial/partOne);
$viee .= view('partial/partTwo');

..
echo $parser->setData($data)->render($view);


RE: How to use LayoutView with parser? - ikesela - 02-10-2022

you should create layout file: layout.php
Code:
<?=view('app/view/header'); ?>
<?= $this->renderSection('main') ?>
<?=view('app/view/footer'); ?>

file to view when call from route : content.php
Code:
<?= $this->extend('app/view/layout) ?>
<?= $this->section('main') ?>
// put your content here to display ,   $data1 , $data2 as variable here
<?= $this->endSection() ?>

controller (example):
Code:
function your_function(){
$data1 = [];
$data2 = [];
return view('app/view/content',[ 'data1' => $data1,'data2' => $data2]);
}

routes.php file: ( example Home class, route /content)
$routes->get('content', 'Home::your_function');