Laravel 在 Blade 模板中转义所有 HTML

2022-01-08 00:00:00 php laravel laravel-blade laravel-4

我正在 Laravel 中构建一个小型 CMS,并尝试显示内容(存储在数据库中).它显示 HTML 标签而不是执行它们.就像所有打印数据都有一个自动 html_entity_decode 一样.

I'm building a small CMS in Laravel and I tried to show the content (which is stored in the DB). It is showing the HTML tags instead of executing them. Its like there is an auto html_entity_decode for all printed data.

<?php

class CmsController extends BaseController
{
    public function Content($name)
    {    
        $data = Pages::where('CID', '=', Config::get('company.CID'))
            ->where('page_name', '=', $name)
            ->first();

        return View::make('cms.page')->with('content', $data);
    }
}

我尝试使用花括号打印内容.

I tried to print the content using the curly brace.

{{ $content->page_desc }}

和三重花括号.

{{{ $content->page_desc }}}

他们给出了相同的结果.我需要执行那些 HTML 标签而不是转义它们.

And they give the same result. I need to execute those HTML tags instead of escaping them.

推荐答案

将语法从 {{ }} 更改为 {!!!!}.

Change your syntax from {{ }} to {!! !!}.

正如 Alpha 在上面的评论中所说(不是答案,所以我想我会发布),在 Laravel 5 中,{{ }}(以前是非转义的输出语法)已经改变到 <代码>{!!!!}.将 {{ }} 替换为 {!!!!} 它应该可以工作.

As The Alpha said in a comment above (not an answer so I thought I'd post), in Laravel 5, the {{ }} (previously non-escaped output syntax) has changed to {!! !!}. Replace {{ }} with {!! !!} and it should work.

相关文章