Laravel - Blade comments , blade rendering causing page to crash Laravel - Blade comments , blade rendering causing page to crash php php

Laravel - Blade comments , blade rendering causing page to crash


Note: this answer was given for Laravel 4.2, but should still apply. There are some special cases of Blade compilation issues that are dependent on the version of Laravel and/or PHP, so it's best to only use Blade comments for the simplest use-cases.

The solution is to only use Blade comments for simple remarks, or to comment out single-line Blade functions. Do not nest Blade/PHP code inside of Blade comments. Use standard PHP block comments to comment out multiple lines of code within a single comment (PHP, HTML, multiple blade functions, etc.).


Valid Blade Comments:

Single Blade Function:

{{-- Form::text('foo') --}}

Remark:

{{-- Form Section 1 --}}

Invalid Blade Comments:

Incorrect syntax:

{{-- Form::text('foo') --  }} 

"@" Inside of Blade comment

{{-- @Form::text('foo') --}} 

Nested PHP:

{{-- <?php echo "foo";echo "bar?> --}} 

Nested Blade:

{{-- {{ HTML::form("foo") }};{{ HTML::form("bar") }};--}} 

Use PHP Block Comments instead. They are still usable in a blade.php file

<?php /* {{ HTML::form("foo") }};{{ HTML::form("bar") }};*/ ?> 

Alternatively, comment out your Blade one line at a time:

{{-- HTML::form("foo") --}};{{-- HTML::form("bar") --}};

Internals:

For the OP's code, Laravel's Blade Compiler will generate a temporary PHP file containing the following PHP/HTML:

<?php /*     <div class="form-row">      <?php echo Form::label('foo', 'foo:'); ?><?php echo Form::text('foo'); ?></div><div class="form-row">    <?php echo Form::label('foo', 'foo:'); ?>    <?php echo Form::text('foo'); ?></div><div class="form-row">    <?php echo Form::label('foo', 'foo'); ?>    <?php echo Form::text('foo'); ?></div>*/ ?>

The Blade inside of your Blade comments are still being parsed into PHP. The PHP end tags inside of the PHP block-comment is causing the Apache's parser to end early, resulting in some badly-formed PHP/HTML that could be crashing your connection (likely caused by the dangling */ ?>).

?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that.

Using any of the aforementioned invalid Blade comments will cause similar compilation issues. Avoid Blade comments for anything other than remarks or commenting Blade functions out one line at a time.


Comments in Blade are very simple!

{{-- Blade comments that wil not appear in the rendered HTML output --}}

You can either do normal PHP comments:

<? /* some comment here */// or single line comments# or these :)?>


I have a similar symptom and it seems to be related to the length of the comment alone. I tested it with a comment that doesn't contain any PHP code or blade statements at all:

{{--0123456789abcdef0123456789abcdef0123456789abcdef--}}

I kept adding copies of the repeated line until it crashed. The comment was lexically followed by a blade @if statement, and the corresponding <php if(...): ?> did not end up in the compiled template, but the closing <?php endif; ?> did, resulting in a syntactically invalid compiled template.

It seems to be a bug in the blade compiler and I will report it.

The workaround is to split long blade comments with --}}{{--.