CodeIgniter Forums
Parse HTML content in CodeIgniter 4 - 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: Parse HTML content in CodeIgniter 4 (/showthread.php?tid=78872)



Parse HTML content in CodeIgniter 4 - ciddict - 03-20-2021

I'm using Codeigniter 4.

PHP Code:
$template 'Hello, {firstname} {lastname}';
$data = [
    'title' => 'Mr',
    'firstname' => '<h1>John</h1>',
    'lastname' => 'Doe'
];

$parser = \Config\Services::parser();
echo 
$parser->setData($data)->renderString($template); 

This is producing,

[Image: 7QB0n.png]
But adding htmlspecialchars_decode() with this is working properly.

PHP Code:
echo htmlspecialchars_decode($parser->setData($data)->renderString($template)); 


[Image: 67Lgu.png]
My question is, is it the right way? Why I need to add this raw function? Is there any parameter or something for this? This was working in CI3.

Thanks in advance.


RE: Parse HTML content in CodeIgniter 4 - demyr - 03-21-2021

As far as I see on the documentation page, html tags are used within views  documentation page


RE: Parse HTML content in CodeIgniter 4 - ciddict - 03-21-2021

@demyr, So there is no way to do that like CI3?


RE: Parse HTML content in CodeIgniter 4 - iRedds - 03-24-2021

{!firstname!}


RE: Parse HTML content in CodeIgniter 4 - includebeer - 03-28-2021

It would make more sense to put the h1 tags in the template, not in the data array, and I think you wouldn't have this problem.

PHP Code:
$template 'Hello, <h1>{firstname}</h1> {lastname}';
$data = [
    'title' => 'Mr',
    'firstname' => 'John',
    'lastname' => 'Doe'
];