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!
Could you post us your code snippet?
Would be a lot easier to solve your problem. Thanks!
Regards,
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.
Is this a job for Blade::compileString()??
I have never used/seen that.
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).
Ah - fundamental. Well that explains it! Thanks, will head in that direction.
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,
Hey @fabricecw ,
Truthy, that's exactly the direction I took.
Thanks all!
Please sign in or create an account to participate in this conversation.