CodeIgniter Forums
How to load partials with View Parser? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: How to load partials with View Parser? (/showthread.php?tid=76219)



How to load partials with View Parser? - YanKleber - 04-22-2020

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!


RE: How to load partials with View Parser? - YanKleber - 04-23-2020

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


RE: How to load partials with View Parser? - InsiteFX - 04-23-2020

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.


RE: How to load partials with View Parser? - YanKleber - 04-23-2020

(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