phplaravellaravel-blade

Dispay Laravel blade content to user as is


I have laravel template.blade.php

<div class="classone">
    <div class="classtwo">
        text
    </div>

    <div class="classtwo">
        text
    </div>
</div>

And I would like to display the code to the user as it is. Incluing new line and indents.

I was playing around with {!! !!}, nl2br(view()->render() even Blade::compileString but was unable to find an elegant solution. Everytime I was able to make it work it was difficult to maintain and every small change to the displayed code was laber intense.

I would like to ask for a suggeston how to display more complex html/css/js code to user. I though it will be fairy often topic but was unable to find anything which would help me.

Thank you in advance.


Solution

  • I tried some things out. They may seem a little bit 'hacky' but I think they will suit your purpose. I used a freshly created Laravel 8 application as an example.

    <pre>{{ file_get_contents( resource_path('views/welcome.blade.php')) }}</pre>
    

    Blade file output

    You can use the Blade facade to compile your blade file to plain php if you want:

    <pre>{{ Blade::compileString(file_get_contents( resource_path('views/app.blade.php'))) }}</pre>
    

    PHP file output

    I put <pre></pre> tags around the output to show line breaks. It makes the code more readable.