Laravel - Blade 评论,刀片渲染导致页面崩溃 [英] Laravel - Blade comments , blade rendering causing page to crash

查看:16
本文介绍了Laravel - Blade 评论,刀片渲染导致页面崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

--}}

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

有人知道这是什么原因造成的吗?如果这很重要,请在 WAMP 上运行.

解决方案

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

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

<小时>

有效的刀片评论:

单刀功能:

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

备注:

{{-- Form Section 1 --}}

<小时>

刀片评论无效:

语法错误:

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

@"刀片内部评论

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

嵌套 PHP:

{{-- <?php回声富";回声酒吧?>--}}

嵌套刀片:

{{--{{ HTML::form("foo") }};{{ HTML::form("bar") }};--}}

<小时>

改用 PHP 块注释.它们仍然可以在blade.php 文件中使用

<小时>

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

{{-- HTML::form("foo") --}};{{-- HTML::form("bar") --}};

<小时>

内部:

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

<?php echo Form::text('foo');?>

<div class="form-row"><?php echo Form::label('foo', 'foo:');?><?php echo Form::text('foo');?>

<div class="form-row"><?php echo Form::label('foo', 'foo');?><?php echo Form::text('foo');?>

*/?>

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

<块引用>

?> 退出 PHP 模式并返回 HTML 模式,并且//或 #不能影响那个.

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

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.

解决方案

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.

这篇关于Laravel - Blade 评论,刀片渲染导致页面崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆