简体   繁体   中英

PHP Return HTML (Laravel / Lumen Framework)

I have a helper function with the following

Note I am using the following to parse content

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.

rawhtml

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

{!! $contents !!} 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM