php Laravel - Blade 评论,刀片渲染导致页面崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27830200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:33:06  来源:igfitidea点击:

Laravel - Blade comments , blade rendering causing page to crash

phplaravelwebblade

提问by z0d14c

I'm rendering a page that is primarily a form with view::makein 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

我正在渲染一个主要是view::makeLaravel 中的表单的页面,并且它崩溃了,导致 ERR_CONNECTION_RESET。经过长时间的调查和许多红鲱鱼,我开始擦除(不评论)刀片文件中的随机部分以供查看,并意识到如果我

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

a) 删除{{Form}}表单此部分中的2 个调用

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

b){{-- and --}}从表格的这一部分周围删除

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

页面将呈现。我不确定这里的确切原因是什么。上面和下面还有其他块,尽管这是一个 3-div 注释掉的部分,其他块都没有。

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

任何人都知道是什么导致了这种情况?如果这很重要,请在 WAMP 上运行。

回答by TonyArra

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.

注意:这个答案是针对 Laravel 4.2 给出的,但仍然适用。Blade 编译问题的一些特殊情况取决于 Laravel 和/或 PHP 的版本,因此最好只对最简单的用例使用 Blade 注释。

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.).

解决方法是只对简单的注释使用 Blade 注释,或者注释掉单行 Blade 函数。不要在 Blade 注释中嵌套 Blade/PHP 代码。使用标准的 PHP 块注释来注释掉单个注释中的多行代码(PHP、HTML、多个刀片函数等)。



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:

{{-- <?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 块注释。它们仍然可以在blade.php 文件中使用

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


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

或者,一次注释掉你的 Blade:

{{-- 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:

对于 OP 的代码,Laravel 的 Blade Compiler 将生成一个包含以下 PHP/HTML 的临时 PHP 文件:

<?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 */ ?>).

Blade 注释中的 Blade 仍在被解析为 PHP。PHP 块注释中的 PHP 结束标记导致 Apache 的解析器提前结束,从而导致一些格式错误的 PHP/HTML 可能会导致您的连接崩溃(可能是由 dangling 引起的*/ ?>)。

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

?> 退出 PHP 模式并返回到 HTML 模式,并且 // 或 # 不能影响它。

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.

使用上述任何无效的 Blade 注释都会导致类似的编译问题。避免将 Blade 注释用于评论或评论 Blade 一次一行的功能以外的任何内容。

回答by Yevgeniy Afanasyev

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

我对 laravel 5.1 和 PHP 7(新宅基地)有同样的问题。解决方法是使用这个:

<?php /* XXX */?>

instead of this:

而不是这个:

{{-- XXX -- }}.

回答by Manojkiran.A

I have Tried the

我试过了

Nested PHP:

嵌套的 PHP:

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

@TonyArra

@托尼阿拉

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

在使用 . 它不评论内容并阻止编译为HTML

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

这是 htmlsource {{-- foobar --}}

Which i have got

我有

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

那是因为如果你想注释Blade里面的php代码

Try this

尝试这个

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

OR

或者

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

and try to view the page source

并尝试查看页面源

回答by Ali Abbas

Comments in Blade are very simple!

Blade中的评论很简单!

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

You can either do normal PHP comments:

你可以做普通的 PHP 注释:

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

回答by Szczepan Ho?yszewski

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:

我有类似的症状,似乎仅与评论的长度有关。我用完全不包含任何 PHP 代码或刀片语句的注释对其进行了测试:

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

I kept adding copies of the repeated line until it crashed. The comment was lexically followed by a blade @ifstatement, 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.

我不断添加重复行的副本,直到它崩溃。注释在词法上跟在一个blade@if语句之后,相应的<php if(...): ?>没有出现在编译模板中,但是结尾出现<?php endif; ?>,导致编译模板在语法上无效。

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

这似乎是刀片编译器中的一个错误,我会报告它。

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

解决方法是使用 --}}{{-- 拆分长刀片注释。

回答by Mladen Janjetovic

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

像这样的 Blade 评论是我的问题所在:

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