Welcome Guest, Not a member yet? Register   Sign In
How to load partials with View Parser?
#1
Question 

Hi!

I am learning CI4 and I got to the point that I was waiting for: View Parser. I am making some tests but am having a hard time trying to include partials with it. This is my pathetic attempt...

The View (produtos_testeForeach.php):

Code:
{header}
<div>
    <h1>{titulo}</h1>
    <hr>
    <table>
        <tr>
            <th><h2>PRODUTO</h2></th>
            <th><h2>PREÇO</h2></th>
        </tr>
        {conteudo}
        <tr>
            <td>{produto}</td>
            <td>{preco}</td>
        </tr>
        {/conteudo}
    </table>
</div>
</body>
</html>

The Controller:

PHP Code:
public function testeForeach() {
        $data['titulo'] = 'Meu titulo';
        // Produtos_header.php is an existing view that I tried to render into a variable
        $header view('produtos_header',$data);

        $parser = \Config\Services::parser();
        $data = [
            // Then I tried to load the contents of this var into array 'header' entry
            'header' => $header,
            'titulo' => 'Isso é um título de teste',
            'conteudo' => [
                ['produto' => 'produto1''preco' => '10,00'],
                ['produto' => 'produto2''preco' => '20,00'],
                ['produto' => 'produto3''preco' => '30,00'],
                ['produto' => 'produto4''preco' => '40,00'],
                ['produto' => 'produto5''preco' => '50,00'],
            ]
        ];
        return $parser->setData($data)->render('produtos_testeForeach');
    

OK... it ALMOST worked. LoL!  Tongue 

The problem is that when it rendered the 'produtos_testeForeach' view instead to render the header partial as HTML it is showing it on screen as plain text:

Code:
<!-- DEBUG-VIEW START 1 APPPATH/Config/../Views/produtos_header.php --> <html> <head> <title>Meu titulo</title> </head> <body> <div id="header"> <h1>Sistema de Controle de Produtos</h1> <hr> </div> <!-- DEBUG-VIEW ENDED 1 APPPATH/Config/../Views/produtos_header.php -->

How can I fix it, aka, how can I load partial views into a view?

Thanks!
Recovering the wasted time...
Reply
#2

Okay, I figured it myself.

I thought about this overnight and this morning I took a look in the source that was being generated (something I didn't think of last night when I started this thread). I realized that the parser was escaping the special characters turning < and > into &lt; and &gt respectively -- and that's why it was showing code as plain text instead to interpret it as HTML.

The solution was fairly simple. Just had to apply the htmlspecialchars_decode native function on parser var this way:

PHP Code:
return htmlspecialchars_decode($parser->setData($data)->render('produtos_testeForeach')); 

Maybe there is simpler ways of solving it (like setting some configuration in CI or so) but it fixed the issue for me!

Big Grin
Recovering the wasted time...
Reply
#3

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

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

(This post was last modified: 04-23-2020, 12:57 PM by YanKleber.)

(04-23-2020, 07:58 AM)InsiteFX Wrote: Using the Parser, 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.

Sure!  Cool
Recovering the wasted time...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB