Laravel Escaping All HTML in Blade Template

后端 未结 6 1624
迷失自我
迷失自我 2020-11-30 07:01

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

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 07:38

    There is no problem with displaying HTML code in blade templates.

    For test, you can add to routes.php only one route:

    Route::get('/', function () {
    
            $data = new stdClass();
            $data->page_desc
                = 'aaabbb
                   

    New paragaph

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

    and in hello.blade.php file:

    
    
    
        
    
    
    
    {{ $content->page_desc }}
    
    
    
    

    For the following code you will get output as on image

    Output

    So probably page_desc in your case is not what you expect. But as you see it can be potential dangerous if someone uses for example '` tag so you should probably in your route before assigning to blade template filter some tags

    EDIT

    I've also tested it with putting the same code into database:

    Route::get('/', function () {
    
            $data = User::where('id','=',1)->first();
    
            return View::make('hello')->with('content', $data);
        }
    );
    

    Output is exactly the same in this case

    Edit2

    I also don't know if Pages is your model or it's a vendor model. For example it can have accessor inside:

    public function getPageDescAttribute($value)
    {
        return htmlspecialchars($value);
    }
    

    and then when you get page_desc attribute you will get modified page_desc with htmlspecialchars. So if you are sure that data in database is with raw html (not escaped) you should look at this Pages class

提交回复
热议问题