Laravel - Blade comments , blade rendering causing page to crash

PhpLaravelWebBlade

Php Problem Overview


I'm rendering a page that is primarily a form with view::make in Laravel and it is crashing, causing ERR_CONNECTION_RESET. After a long investigation and many red herrings, I started erasing (not commenting) random sections out of the blade file for the view and realized that if I

a) erase 2 of the {{Form}} calls inside this section of the form

b) remove the {{-- and --}} from around this section of the form

    {{--
    <div class="form-row">
      {{ Form::label('foo', 'foo:') }}
      {{ Form::text('foo') }}
    </div>
    <div class="form-row">
      {{ Form::label('foo', 'foo:') }}
      {{ Form::text('foo') }}
    </div>
    <div class="form-row">
      {{ Form::label('foo', 'foo') }}
      {{ Form::text('foo') }}
    </div>
    --}}

the page will render. I am not sure what exactly the cause here is. There are other blocks above and below, although this is a 3-div commented out section which none of the others are.

Anyone have a clue what is causing this? Running on WAMP if that matters.

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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 :)
?>

Solution 3 - Php

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:

{{--
0123456789abcdef
0123456789abcdef
0123456789abcdef
--}}

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 --}}{{--.

Solution 4 - Php

I have same problem with laravel 5.1 and PHP 7 (new homestead). The work around was to use this:

<?php /* XXX */?>

instead of this:

{{-- XXX -- }}.

Solution 5 - Php

I have Tried the

Nested PHP:

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

> @TonyArra

While using . It is Not Commenting the Content and prevents from Compiling as HTML

and this is the htmlsource {{-- foobar --}}

Which i have got

Thats Because If You want To Comment the php Code inside Blade

Try this

<!-- @php echo 'hai'; @endphp -->

OR

<!-- <?php echo 'hai'; ?> -->

and try to view the page source

Solution 6 - Php

Blade comments like this one, were the problem in my case:

{{--	
    @if ($test)
	 	<div>something</div>
    @else
        <div>something else</div>
    @endif
--}}

Solution 7 - Php

Simply we have to use a double curly bracket followed by a double hyphen. This will work for the single line as well multiple lines. {{-- --}}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionz0d14cView Question on Stackoverflow
Solution 1 - PhpTonyArraView Answer on Stackoverflow
Solution 2 - PhpAli AbbasView Answer on Stackoverflow
Solution 3 - PhpSzczepan HołyszewskiView Answer on Stackoverflow
Solution 4 - PhpYevgeniy AfanasyevView Answer on Stackoverflow
Solution 5 - PhpManojKiran AppathuraiView Answer on Stackoverflow
Solution 6 - PhpMladen JanjetovicView Answer on Stackoverflow
Solution 7 - PhpRahul Kumar TiwariView Answer on Stackoverflow