Welcome Guest, Not a member yet? Register   Sign In
How to use LayoutView with parser?
#1

(This post was last modified: 08-14-2020, 11:04 AM by jreklund.)

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

I do not think it will work because they are two different entities.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3
Sad 
(This post was last modified: 08-14-2020, 02:56 PM by jreklund.)

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

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

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 08-21-2020, 01:19 PM by jreklund.)

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

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

(This post was last modified: 07-22-2021, 07:26 PM by Ilham Fadil.)

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

i'm using this trick.

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

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

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');
Reply




Theme © iAndrew 2016 - Forum software by © MyBB