Welcome Guest, Not a member yet? Register   Sign In
Parse HTML content in CodeIgniter 4
#1

(This post was last modified: 03-20-2021, 11:52 PM by ciddict.)

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

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

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

{!firstname!}
Reply
#5

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'
]; 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB