Blade,在传递到局部视图的变量中使用 html 没有被渲染
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29470478/
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
Blade, Use html inside variable being passed into partial view is not being rendered
提问by Mohamed Kawsara
I'm using a partial view to display page header, that view accepts two variables one is Icon the other is a title. pageHeader.blade.php:
我使用局部视图来显示页眉,该视图接受两个变量,一个是图标,另一个是标题。pageHeader.blade.php:
<div class="page-header">
<div class="row">
<!-- Page header, center on small screens -->
<h1 class="col-xs-12 col-sm-4 text-center text-left-sm"><i class="fa {{$icon}} page-header-icon"></i> {{$title}}</h1>
</div>
and I'm using it like so:
我是这样使用它的:
@include('zdashboard._partials.pageHeader',['icon'=>'fa-pencil','title'=>'<strong>Editing</strong>'.$center->translations()->whereLang('en')->first()->name])
Sometimes I like to make one word strong or italic like the example above but, blade engine won't render the HTML tags I'm typing as part of title
variable (the output like the photo down).
有时我喜欢像上面的例子一样使一个词变强或斜体,但是,刀片引擎不会将我输入的 HTML 标签呈现为title
变量的一部分(输出像照片一样)。
So is there any idea how to solve this? Am I doing it!
那么有什么想法如何解决这个问题吗?我在做吗!
wrong?
错误的?
回答by Limon Monte
By default in Laravel 5 {{ $title }}
construction wil be escaped.
默认情况下,Laravel 5 的{{ $title }}
构造将被转义。
If you don't want the data to be escaped, you may use the following syntax:
如果您不希望数据被转义,您可以使用以下语法:
{!! $title !!}
Read more about Blade control structures: http://laravel.com/docs/5.0/templates#other-blade-control-structures
阅读有关 Blade 控制结构的更多信息:http: //laravel.com/docs/5.0/templates#other-blade-control-structures
回答by Sharma Ji
view
看法
@if(Session::has('success'))
<div class="alert alert-success">
{!! Session::get('success')!!}
</div>
@endif
Controller
控制器
public function store(Request $request)
{
$this->validate($request, [
'product_code' => 'required|unique:products',
'product_name' => 'required',
'description' => 'required',
'price' => 'required',
'brand_id' => 'required',
'category_id' => 'required',
]);
$product = Product::create([
'product_code' => $request->input('product_code'),
'product_name' => $request->input('product_name'),
'description' => $request->input('description'),
'price' => $request->input('price'),
'brand_id' => $request->input('brand_id'),
'category_id' => $request->input('category_id'),
]);
return redirect()->route('products.index')->with('success',
"The product <strong>".$product->product_name."</strong> has successfully been created.");
}