简体   繁体   中英

Display code snippet in Laravel blade

i need to display as text on the screen a snippet that contains some php variables, but when i load the page Laravel resolve this variables and give me a undefined variable error. Basically i need that when i try to display the snippet inside <code></code> tags it print something like this:

<script type='text/javascript>
    var variable = '<?= $variable ?>';
</script>

I have tried already with {{htmlspecialchars}} but not work

In your controller you can assign the code to a variable :

$var = Blade::compileString('<script type=\'text/javascript\'>
    var variable = \'<?= $variable ?>\';
</script>');

In your blade, access the variable as :

{{ $var }}

You can use highlight_string($code) to show the snippet. Inspiration from this post: How do I display PHP code in HTML?

Maybe you could try to enclose your snippet on a @verbatim block. This should escape its content of the rendering.

See more here: https://laravel.com/docs/5.5/blade#displaying-data

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