PHP 返回 HTML (Laravel / Lumen 框架)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30007139/
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-09-14 11:28:55  来源:igfitidea点击:

PHP Return HTML (Laravel / Lumen Framework)

phplaravellumen

提问by kevingilbert100

I have a helper function with the following

我有以下辅助功能

NoteI am using the following to parse content

注意我正在使用以下内容来解析内容

https://github.com/erusev/parsedown

https://github.com/erusev/parsedown

function display_docs_page($name){

    // Get Docs URL
    $docs_url = config('docs.docs_url');

    // Get File URL
    $file_url = $docs_url.'/'.$name.'.md';

    // Check If File Exists
    if (file_exists($file_url)) {

        // get raw file data
        $raw_file_data = file_get_contents($file_url);

        // convert data to html
        $parsedown = new Parsedown();

        return $parsedown->text($raw_file_data);

    } else {

        // 404
        return 'not_found';

    }

}

However when I run my function

但是,当我运行我的函数时

return view('greeting', [
     'contents' => display_docs_page(config('general.homepage')),
]);

and try to echo out the variable so in blade

并尝试在刀片中回显变量

{{ $contents }}

I just just RAW html code. So its displaying the html data but its just raw code the browser isn't interpreting it.

我只是 RAW html 代码。所以它显示 html 数据,但它只是浏览器没有解释的原始代码。

rawhtml

原始 HTML

回答by Limon Monte

In Lumen / Laravel 5 you should use {!! !!}to output variable without escaping:

在 Lumen / Laravel 5 中,您应该使用{!! !!}输出变量而不转义:

{!! $contents !!} 

Read more: http://laravel.com/docs/master/upgrade#upgrade-5.0(Blade Tag Changes section)

阅读更多:http: //laravel.com/docs/master/upgrade#upgrade-5.0(刀片标签更改部分)

回答by mcklayin

Its because by default laravel escaped everything in {{ }}, you must use {!! $contents !!}instead of {{ $contents }}

这是因为默认情况下,laravel 会转义 中的所有内容{{ }},您必须使用{!! $contents !!}而不是{{ $contents }}