php Laravel 5 在刀片中回显包含 html 的会话变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32172241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:49:10  来源:igfitidea点击:

Laravel 5 echo out session variable containing html in blade

phplaravellaravel-5blade

提问by Webtect

I did a redirect in laravel:

我在 Laravel 中做了一个重定向:

return redirect('admin')->with($returnData);

$returnData is a string that contains a bootstrap info div with the result from the controller. Almost everything is working except when the page loads again it shows the html on the page as if it were text, brackets and everything. If I use this:

$returnData 是一个字符串,其中包含一个引导信息 div,其中包含来自控制器的结果。几乎一切正常,除非页面再次加载时,它会在页面上显示 html,就好像它是文本、括号和所有内容一样。如果我使用这个:

@if(!empty(Session::get('error'))) {{ Session::get('error')}} @endif

Then it shows is as pure text. If I change it to

然后它显示为纯文本。如果我把它改成

<?php if(!empty(Session::get('error'))) { echo Session::get('error'); } ?>

It works fine. Im ok keeping it like this but I would rather utilize Blade / Laravel as its supposed to be used so I was wondering if there is a way to have the @if statement show the rendered html and not the text version?

它工作正常。我可以保持这样,但我宁愿使用 Blade / Laravel 作为它应该使用的所以我想知道是否有办法让 @if 语句显示呈现的 html 而不是文本版本?

回答by mdamia

I would recommend returning just the error message then in your view create the div. So if you were to change the layout of the view, you would it in one place.

我建议只返回错误消息,然后在您的视图中创建 div。所以如果你要改变视图的布局,你会在一个地方。

@if(Session::has('error'))
<div class="alert alert-danger">
  {{ Session::get('error')}}
</div>
@endif

hope this help.

希望这有帮助。

回答by Guillermo Lobos Parada

To show the rendered HTML you should use {!! $variable->coontent !!}in your view, and this gonna convert your HTML text to render

要显示您应该{!! $variable->coontent !!}在视图中使用的呈现的 HTML ,这会将您的 HTML 文本转换为呈现

回答by Saeed

May this example will help you. Try this

愿这个例子对你有所帮助。尝试这个

 @if (session('Error'))
     <div class="alert alert-success">
         {{ session('Error') }}
     </div>
@endif

回答by Subash

Try changing your blade code to following.

尝试将您的刀片代码更改为以下。

@if(!empty(Session::get('error'))) 
    {!! Session::get('error') !!} 
@endif