Skip to content

Instantly share code, notes, and snippets.

@edgrosvenor
Created November 23, 2020 20:06
Show Gist options
  • Save edgrosvenor/8acc43ee32edb45cfb679fdecac52117 to your computer and use it in GitHub Desktop.
Save edgrosvenor/8acc43ee32edb45cfb679fdecac52117 to your computer and use it in GitHub Desktop.
Displaying blade component tags in a code block in Laravel
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class CodeBlock extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return function($data) {
return '<code><pre>' .
str_replace(['<x-', '</x-'], ['&lt;x-', '&lt;/x-'], (string) $data['slot'])
. '</code></pre>';
};
}
}
<x-code-block>
@verbatim
<x-sentence class="classy">
This will display the contents of the code block as expected without attempting to compile the sentence component.
</x-sentence>
@endverbatim
</x-code-block>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment