62

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.

Set Kyar Wa Lar
  • 4,488
  • 4
  • 30
  • 57
z0d14c
  • 1,122
  • 1
  • 12
  • 20
  • @maytham I do not quite understand what a master comment is, I suppose. Are you implying that "master commenting" is what is causing the page to crash? – z0d14c Jan 08 '15 at 16:30
  • ok if we start over, have your code worked correct before or it is while you are working on a new project? because if it is new project then I want to know if this form will have relation to a database? if yes, is it for inserting or editing data or both, i need to know some facts to tell how to approach. – Maytham Fahmi Jan 08 '15 at 22:32

8 Answers8

74

Blade comments should only be used for simple remarks or to comment out single-line Blade functions. A single Blade comment cannot be used to comment out multiple lines of code.


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") --}};

Examples of Valid Blade Comments:

Single Blade Function:

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

Remark:

{{-- Form Section 1 --}}

Examples of 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") }};
--}} 

Internals:

Using the sample code from the question, Laravel's Blade Compiler will generate a temporary PHP file containing the following PHP and 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 code inside of the Blade comments are still parsed into PHP. The PHP end tags inside of the PHP block-comment can cause compilation issues:

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

TonyArra
  • 10,607
  • 1
  • 30
  • 46
13

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 :)
?>
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Ali Abbas
  • 299
  • 2
  • 11
3

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 -- }}.
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
2

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

Szczepan Hołyszewski
  • 2,707
  • 2
  • 25
  • 39
  • 1
    I am pretty sure you and I are hitting the same bug, but I am not 100% sure it's the same bug as in the original question. The symptom is similar, but what makes me doubt that the cause is shared is the fact that in my application this problem only started occurring since the move to PHP 7, which is consistent with your experience. So it might well be that PHP version is relevant after all, just not the way TonyArra believed. (1/2) – Szczepan Hołyszewski Jan 06 '16 at 11:20
  • I haven't examined the blade compiler code yet, but I suspect that blade comments handling is implemented more or less as a hack that modifies the input stream, and it miscalculates the length of the fragment it needs to replace. A strong hint towards this is the fact that a newline always seems to count as 2 characters towards the experimentally determined breakage limit, even though I am on Linux and my newlines are just `\n`, not `\r\n`. (2/2) – Szczepan Hołyszewski Jan 06 '16 at 11:20
  • Oh, if you still have this environment, can you please try Laravel 5.2, I have heard it fixes the issue. I've gone back to previous homestead and it is too much hustle for me to give it a try. – Yevgeniy Afanasyev Jan 07 '16 at 02:26
  • And you are right, it might be another issue in the initial question, because it was asked a year ago, there was no PHP-7 at that time. Thanks. – Yevgeniy Afanasyev Jan 07 '16 at 02:27
  • Sorry I cannot confirm your answer as our codebase has changed and the bug hasn't occurred in a long while. – z0d14c Jan 07 '16 at 18:41
0

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

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
-1

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

{{--    
    @if ($test)
        <div>something</div>
    @else
        <div>something else</div>
    @endif
--}}
Mladen Janjetovic
  • 13,844
  • 8
  • 72
  • 82
-1

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

-1

Blade Comments

{{-- This comment will not be present in the rendered HTML --}}

Referene: https://laravel-news.com/laravel-blade-comments