Php Laravel刀片模板渲染2次

Php Laravel刀片模板渲染2次,php,laravel-4,blade,Php,Laravel 4,Blade,以这个(非)blade脚本list.blade.php: <?php error_log( print_r( "START" , true ) ); global $i; for ( $a = 0 ; $a < 3 ; $a++ ) { $j = @(int)$i; error_log( print_r( $j , true ) ); echo $j; $i = $j + 1 ; } error_log( print_r( "STOP" ,

以这个(非)blade脚本
list.blade.php

<?php
error_log( print_r( "START" , true ) );

global $i;

for ( $a = 0 ; $a < 3 ; $a++ ) {
    $j = @(int)$i;

    error_log( print_r( $j , true ) );
    echo $j;

    $i = $j + 1 ;
}

error_log( print_r( "STOP" , true ) );
因此,第一次运行模板时不输出任何内容,第二次运行模板后发送输出

我使用更新的Laravel 4.2版本。这并不是一个真正的问题,但是当每个解析的行请求繁重的计算任务时,加载时间仅为X2

你认为这是一个bug还是一个正常的行为


有没有办法避免在第一次启动(干运行)时在模板中执行某些操作?

这不是答案。我刚刚用你的代码进行了测试,结果是
012
。我用版本4.2.6测试了它。我不知道你是怎么得到它的

这是我使用的代码

app/route.php

Route::get("test",function(){
    return View::make("aaa");
});
app/views/aaa.blade.php

<?php
error_log( print_r( "START" , true ) );

global $i;

for ( $a = 0 ; $a < 3 ; $a++ ) {
    $j = @(int)$i;

    error_log( print_r( $j , true ) );
    echo $j;

    $i = $j + 1 ;
}

error_log( print_r( "STOP" , true ) );

找到了问题,但没有找到解决方案!我用我的拉威尔样板,但这是罪魁祸首。我使用全局后过滤器动态缩小HTML代码(添加了此问题的错误日志):

在以下行上对叶片模板进行第二次评估:

$output = preg_replace( $re , " " , $output );

这在PHP中是不可能的。。。我想我错过了拉威尔的建筑设计。。。如果有人了解任何东西…

您在哪里包括此模板?而使用global简直让我抓狂。如果您想传入额外的数据,请使用decorators。此模板只是一个PoC。“全局”只是用来在几个负载之间保持状态,以向您显示问题。您是对的!我用我的拉威尔样板,但这是罪魁祸首。我使用after过滤器动态缩小HTML代码。我会回答我的问题。
App::after(function ($request, $response) {

    // Minify only texts
    if ( strpos( $response->headers->get('content-type') , 'text/' ) !== false ) {
        if ($response instanceof Illuminate\Http\Response) {

error_log( print_r( 'coucou1' , true ) );
            $output = $response->getOriginalContent();
error_log( print_r( 'coucou2' , true ) );

            $re = '%# Collapse whitespace everywhere but in blacklisted elements.
            (?>             # Match all whitespans other than single space.
            [^\S ]\s*     # Either one [\t\r\n\f\v] and zero or more ws,
            | \s{2,}        # or two or more consecutive-any-whitespace.
            ) # Note: The remaining regex consumes no text at all...
            (?=             # Ensure we are not in a blacklist tag.
            [^<]*+        # Either zero or more non-"<" {normal*}
            (?:           # Begin {(special normal*)*} construct
            <           # or a < starting a non-blacklist tag.
            (?!/?(?:textarea|pre|script)\b)
            [^<]*+      # more non-"<" {normal*}
            )*+           # Finish "unrolling-the-loop"
            (?:           # Begin alternation group.
            <           # Either a blacklist start tag.
            (?>textarea|pre|script)\b
            | \z          # or end of file.
            )             # End alternation group.
            )  # If we made it here, we are not in a blacklist tag.
            %Six';

error_log( print_r( 'coucou3' , true ) );

            $output = preg_replace( $re , " " , $output );

error_log( print_r( 'coucou4' , true ) );
            if ($output !== null) {
                $response->setContent($output);
            }
error_log( print_r( 'coucou5' , true ) );
        }
    }

});
START
0
1
2
STOP
coucou1
coucou2
coucou3
START
3
4
5
STOP
coucou4
coucou5
$output = preg_replace( $re , " " , $output );