anjanesh's avatar

Exception on server but works fine on localhost

I just included @include('partials.homepage-schemas') in my index.blade.php and partials/homepage-schemas.blade.php contains this kind of text :

My localhost works fine with the change but no my server I get :

syntax error, unexpected end of file, expecting "elseif" or "else" or "endif" (View: /home/path/public_html/resources/views/partials/homepage-schemas.blade.php) (View: /home/path/public_html/resources/views/partials/homepage-schemas.blade.php)
0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! This is a very common issue when including raw curly braces ({}) and Blade syntax in view files. On your server, Blade is trying to interpret your JSON as Blade code (like {{ }} and @), especially if your server is set to a stricter parsing configuration or a different version of PHP/Blade.

Why does it work locally?

  • Your local environment may have different versions of PHP or Blade, or more relaxed error reporting.
  • Your server may have upgraded PHP or Blade, making it stricter or handling {{-- and {{ differently.

How to Fix (Best Practice)

1. Use Blade's @verbatim Directive

The @verbatim ... @endverbatim directives tell Blade not to process anything inside. Put this around your schema code:

Why does this work?
Blade will ignore all curly braces and not try to interpret them as variables or Blade directives.


2. Other Recommendations

  • You should quote all property values in JSON (as shown above), even URLs. This prevents syntax errors.
  • When pasting large blocks of HTML or JS into Blade, always guard against accidental {{ ... }} that Blade tries to process.

Summary

Wrap your JSON schema(s) in @verbatim ... @endverbatim in your partial file (and fix any missing or unquoted strings in the JSON as shown). This avoids Blade parsing issues both locally and on servers.

If you need to inject dynamic data, you can still do so with Blade outside of the @verbatim block or build the JSON in PHP/controller and pass as a variable.


Let me know if you need a version that mixes dynamic data!

Glukinho's avatar
Level 30

I guess it's about @context, @type which Blade engine recognizes as directives.

See here: https://laravel.com/docs/13.x/blade#blade-and-javascript-frameworks

The @ symbol may also be used to escape Blade directives:

So I think you should either use double @ :

"@@context": "https://schema.org",
"@@type": "WebSite",

Or wrap your scripts with @verbatim ... @endverbatim which prevents it's inner content to be processed by Blade engine:

<script type="application/ld+json">
@verbatim
    {
        "@context": "https://schema.org",
        "@type": "WebSite",
        "name": "xxx",
        ...
    }
@endverbatim 
</script>

Please or to participate in this conversation.