Laravel 5 - How to show variable within view with

2019-08-26 23:42发布

$var_from_controller = "<div style="color:red">Picture inside html {{ asset('img/photo1.jpg') }} </div>"

@blade

<div id="my-container">
  {!! $var_from_controller !!}
</div>

It will render {{asset()}} as a string. But I need HTML AND asset() output.

How can I do this??

2条回答
Rolldiameter
2楼-- · 2019-08-27 00:08

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)

查看更多
我命由我不由天
3楼-- · 2019-08-27 00:14

In blade file change below code. concatenate image path in variable

@php
    $var_from_controller = "<div style='color:red'>Picture inside html ". asset('img/photo1.jpg') ." </div>";
@endphp

<div id="my-container">
   {!! $var_from_controller !!}
</div>
查看更多
登录 后发表回答