Search code examples
laravelmarkdownlaravel-blade

Show blade code snippet inside a laravel blade view


I am creating documentation for my blade based components in a laravel project and would like to display syntax highlighted blade code snippets as part of the documentation a la something like this:

Desired result

I have installed graham-campbell/markdown package and I try use it inside a .blade.php file as follows:

enter image description here

(Do not mind the escape character)

However, the output I get is as follows:

Actual output


Solution

  • You can wrap the Blade in a @verbatim directive and use Highlight JS with a style you like

    <p>You can use the laravel code template like this</p>
    
    @markdown
    ```php
    @verbatim
    @include('components.inputs.text', [
       'name' => 'input_name',
       'label' => 'testing',
    ])
    @endverbatim
    ```
    @endmarkdown
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/styles/a11y-dark.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/highlight.min.js"></script>
    <script>
        hljs.initHighlightingOnLoad();
    </script>
    

    Result

    enter image description here

    Hope this helps