使用此(非)刀片脚本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 ) );预期结果应该是012,但输出却是345!
如果检查服务器错误日志,您可以看到以下内容:
START
0
1
2
STOP
START
3
4
5
STOP因此,模板在第一次运行时不输出任何内容,第二次运行,然后发送输出。
我使用的是更新的Laravel 4.2版本。这并不是真正的问题,但当每个解析的行都在请求繁重的计算任务时,加载时间只是X2。
你认为这是一个bug,还是正常的行为?
有没有办法避免在第一次启动(预演)时在模板中执行某些操作?
发布于 2014-07-18 18:48:34
发现问题,但没有解决方案!我使用的是Laravel样板,但它是罪魁祸首。我使用一个全局的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 );这在PHP中是不可能的.我想我怀念拉威尔的建筑设计。如果有人明白了什么..。
发布于 2014-07-18 18:08:44
这不是一个答案。我刚刚用你的代码进行了测试,得到了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 ) );https://stackoverflow.com/questions/24821562
复制相似问题