Laravel - Blade comments , blade rendering causing page to crash

后端 未结 6 1883
清歌不尽
清歌不尽 2020-12-13 09:43

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

相关标签:
6条回答
  • 2020-12-13 10:03

    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

    0 讨论(0)
  • 2020-12-13 10:04

    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.

    0 讨论(0)
  • 2020-12-13 10:10

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

    {{--    
        @if ($test)
            <div>something</div>
        @else
            <div>something else</div>
        @endif
    --}}
    
    0 讨论(0)
  • 2020-12-13 10:16

    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 :)
    ?>
    
    0 讨论(0)
  • 2020-12-13 10:18

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

    0 讨论(0)
  • 2020-12-13 10:23

    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 -- }}.
    
    0 讨论(0)
提交回复
热议问题