skip to content

In a Blade view that also contains Vue-style templates, how do @{{ }} and @verbatim stop Blade from compiling the client-side curly braces?

level: juniorimportance: should knowfreq 42%

answer

  1. same braces, two template engines
  2. an @ prefix keeps one echo literal
  3. Blade removes the @ itself
  4. @verbatim ... @endverbatim for blocks
  5. @@if for a literal directive

basics

~20 s

Prefixing an echo with @, as in @{{ name }}, makes Blade drop the @ and leave {{ name }} for the JavaScript framework. Wrapping a region in @verbatim ... @endverbatim leaves everything inside uncompiled, echoes and directives alike.

solid answer

~40 s

Blade and client-side frameworks such as Vue both use `{{ }}`. Blade's echo compiler matches an optional `@` before the opening braces: when it is present, Blade strips the `@` and outputs the rest unchanged, so `@{{ name }}` reaches the browser as `{{ name }}`. The same works for the raw form, and `@@if` prints a literal `@if`. For a large region, `@verbatim ... @endverbatim` is cleaner: Blade stores the block before compiling anything and restores it afterwards, so no echo, directive or component tag inside is touched - which also means you cannot use a Blade variable inside it. Alpine's attribute syntax (`x-text`, `x-data`) does not collide and needs neither.

code

html · 9 lines
html
<h1>{{ $thread->title }}</h1>          {{-- Blade, escaped --}}
<p>Replies: @{{ replyCount }}</p>      {{-- left for Vue --}}

@verbatim
    <template id="reply-form">
        <textarea v-model="draft"></textarea>
        <span>{{ draft.length }} / 500</span>
    </template>
@endverbatim

go deeper

for a junior

Recall the two escapes: @{{ }} for one echo, @verbatim for a block, and @@ for a literal directive.

for a middle

Explain that Blade stores verbatim blocks before compiling and restores them after, so nothing inside - echoes, directives, component tags - is touched.

for a senior

Spot the client-side template injection risk when server-escaped user text lands inside an element a JavaScript framework compiles.

for a principal

Decide how server and client templates share a page - clear mount boundaries and data passed as data - so collisions and injection both stay out.

## Why the collision happens Blade compiles a template on the server; a client-side framework such as **Vue** compiles its own templates in the browser. Both use double curly braces for interpolation. If a Blade file contains a Vue template, Blade sees `{{ message }}` first, compiles it to `<?php echo e(message); ?>`, and PHP fails on the undefined constant `message` before the client framework ever sees the expression. Blade offers two escape hatches. ## The @ prefix for a single echo Blade's echo compiler uses a pattern with an optional leading `@`. When the `@` is present, Blade **removes it and leaves the braces and their contents untouched**: | Blade source | Sent to the browser | |---|---| | `@{{ message }}` | `{{ message }}` | | `@{!! html !!}` | `{!! html !!}` | | `@@if(ready)` | `@if(ready)` | The last row is the directive form: `@@` produces a literal `@` followed by the directive name, which is useful when documentation pages show Blade syntax or when a frontend library uses `@`-prefixed attributes that look like directives. The prefix is per-echo. It suits a page that mostly renders server data and has a few client expressions. ## The @verbatim block for a region ```html @verbatim <div id="thread-app"> <p>{{ post.title }} by {{ post.author }}</p> </div> @endverbatim ``` When compiling, Blade first **extracts every `@verbatim` block and stores it**, then compiles components, comments, directives and echoes in the rest of the template, and finally **restores the stored blocks unchanged**. Consequences: - No `{{ }}`, `{!! !!}`, `@if`, `@foreach`, `<x-...>` component tag or other directive inside the block is compiled. - There is **no way to use a Blade value inside the block**. If the client template needs a server value, close the block, echo the value (for scripts, with `Js::from`), and reopen it - or pass the data once through a script or data attribute outside the block. - Blocks are not nested. ## Which to choose 1. A handful of client expressions in a server-rendered page: `@{{ }}`. 2. A whole client-side widget or template section: `@verbatim`. 3. Documentation showing Blade syntax: `@@` for directives, `@{{ }}` for echoes. 4. Alpine.js markup: usually neither. Alpine reads directives from attributes such as `x-text` and `x-data`, which do not use `{{ }}`. ## The security edge: server-escaped text inside a client template `e()` escapes `<`, `>`, `&` and quotes, but **not curly braces**. If user text such as a forum signature is echoed with `{{ $signature }}` inside an element that a client framework later mounts as a template, text like `{{ someExpression }}` in the signature is compiled by the client framework in the visitor's browser. That is **client-side template injection**: the HTML escaping was correct and the page is still vulnerable. The Blade-side defences: - Render user content **outside** the element the client framework mounts on. - Or pass the data in as data (for example with `Js::from`) and let the client framework render it as text, where its own escaping applies. `@verbatim` does not help here: it stops Blade from compiling braces, not the browser-side framework. ## Common mistakes - Expecting `@{{ }}` to output the `@` as well; Blade consumes it. - Trying to echo a server variable inside `@verbatim` and getting the literal text. - Wrapping Alpine components in `@verbatim` by habit, which then blocks Blade from rendering the server values the component needed. - Using a backslash to escape braces; Blade has no backslash escape. - Assuming HTML escaping also neutralises client-side template syntax. ## Summary `@` in front of an echo or directive is a one-off literal; `@verbatim` is a region-wide one. Both are compile-time instructions to Blade only, and neither changes how the client framework treats what it receives.

  • How do you get a server value into a region wrapped in @verbatim?
    You cannot echo inside it, because Blade does not compile anything there. Close the block, output the value, and reopen it; or better, hand the data to the client framework once outside the block - for a script, with `Js::from` - and let the client template read it.
  • Why can {{ $signature }} be dangerous inside a Vue-mounted element even though e() escaped it?
    `e()` does not encode curly braces. If the element becomes a client template, a signature containing `{{ ... }}` is compiled and evaluated by the client framework in the visitor's browser. Render user text outside the mounted root, or pass it as data so the client renders it as text.

  • @{{ }} outputs the @ sign along with the braces
  • Blade values can still be echoed inside @verbatim
  • e() escaping also protects text a client framework compiles
  • Alpine's x-text attributes must be wrapped in @verbatim
  • A backslash before {{ stops Blade from compiling it