Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RushVan's avatar

Store html string including variable name in db and then insert into blade view.

I have a text field that captures an html string. I want the user to able to also include a variable name like 'Some text and a {{ $variable['name'] }}'. I am then inserting this into a blade view using the escaped html syntax {!! $htmlstring !!}.

However, I can't get the {{ $variable['name'] }} to present the data, it just echos the variable tag {{ $variable['name'] }}.

What am I missing? Thanks!

0 likes
7 replies
fabricecw's avatar

Could you post us your code snippet? Would be a lot easier to solve your problem. Thanks!

Regards,

RushVan's avatar

Sure.

In a text field in my table I have this in the 'html_string' field;

'My string with a {{ $variable['name'] }}'

Then in my view I have this;

<p>{!! $html_string !!}</p>

That renders this;

'My string with a {{ $variable['name'] }}'

Where I want it to render this;

'My string with a variable' // 'variable being the actual value of $variable['name'].

Hope that makes sense. No errors BTW. Thanks.

RushVan's avatar

Is this a job for Blade::compileString()??

I have never used/seen that.

jekinney's avatar
jekinney
Best Answer
Level 47

This is fundamental php here. You're trying to render a php variable while it's being parsed. It's to late to inject more php then.

You'll have to build your string before it's sent to php engine for rendering. (In mvc, before passing data to the view).

RushVan's avatar

Ah - fundamental. Well that explains it! Thanks, will head in that direction.

fabricecw's avatar

I would go an other way:

Store the html_string like this:

My string with a {test}

And pass it to blade this way. Example:

$string = 'This is a {test}';
$output = preg_replace_callback('~\{(.*?)\}~',
            function($key)
            {
                $variable['test'] = 'Banana';
                return $variable[$key[1]];
            },
            $string);
echo $output;

This replaces "This is a {test}" with "This is a banana".

$key[1] returns you the value inside the {}.

Regards,

4 likes

Please or to participate in this conversation.