Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...