PHP Laravel 5 blade 模板中的代码
PHP code inside a Laravel 5 blade template
我必须在 Laravel 5 blade 模板中放置一些 PHP 代码。喜欢下面
@foreach ($farmer->tasks as $task)
@if ($task->pivot->due_at) < date(now))
$style = 'alert alert-danger';
@elseif ($task->pivot->due_at) > date(now))
$style = 'alert alert-success';
@else
$style = '';
@endif
@endforeach
将 PHP 代码放入 Laravel 5 blade 模板的实际过程是什么?
只需打开和关闭 PHP 标签:
<?php $style = '...'; ?>
以下新的 NewBladeCompiler 将使用 @{ }}
接受所有 PHP 代码,如变量分配、class 声明等
例如@{ $variable = 0; }}
将编译为 <?php $variable=0; ?>
<?php
use Illuminate\View\Compilers\BladeCompiler;
class NewBladeCompiler extends BladeCompiler
{
/**
* Get the echo methods in the proper order for compilation.
*
* @return array
*/
function getEchoMethods()
{
$methods = [
'compileRawEchos' => strlen(stripcslashes($this->rawTags[0])),
'compileEscapedEchos' => strlen(stripcslashes($this->escapedTags[0])),
'compileRegularEchos' => strlen(stripcslashes($this->contentTags[0])),
'compilePhpEchos' => strlen(stripcslashes("@{"))
];
uksort($methods, function ($method1, $method2) use ($methods) {
// Ensure the longest tags are processed first
if($methods[$method1] > $methods[$method2])
{
return -1;
}
if($methods[$method1] < $methods[$method2])
{
return 1;
}
// Otherwise give preference to raw tags (assuming they've overridden)
if($method1 === 'compilePhpEchos')
{
return -1;
}
if($method2 === 'compilePhpEchos')
{
return 1;
}
if($method1 === 'compileRawEchos')
{
return -1;
}
if($method2 === 'compileRawEchos')
{
return 1;
}
if($method1 === 'compileEscapedEchos')
{
return -1;
}
if($method2 === 'compileEscapedEchos')
{
return 1;
}
});
return $methods;
}
function compilePhpEchos( $value )
{
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', "@{", "}}");
$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3] . $matches[3];
return $matches[1] ? substr($matches[0], 1) : '<?php ' . $matches[2] . ' ?>' . $whitespace;
};
return preg_replace_callback($pattern, $callback, $value);
}
}
?>
Laravel 食谱提出了一种简单但有效的方法,无需包含 PHP 标签:
{{--*/ $var = 'test' /*--}}
{{-- --}} 作为 blade 评论 / 和 / 恢复对
的评论效果
<?php $var = 'test' ?>
问题是它比包含 PHP 个标签要长:-(
根据 documentation,在 Laravel 5.2 和更高版本中,您可以使用以下代码:
@php
{{-- PHP code here --}}
@endphp
或者,您可以按照 here 的描述扩展 Blade 模板引擎。
如果以上两种解决方案都不合适,您将无法使用给出的答案 and 。
在现代 Laravel (6/7) 你应该这样做:
@php
yourphpcode();
@endphp
我必须在 Laravel 5 blade 模板中放置一些 PHP 代码。喜欢下面
@foreach ($farmer->tasks as $task)
@if ($task->pivot->due_at) < date(now))
$style = 'alert alert-danger';
@elseif ($task->pivot->due_at) > date(now))
$style = 'alert alert-success';
@else
$style = '';
@endif
@endforeach
将 PHP 代码放入 Laravel 5 blade 模板的实际过程是什么?
只需打开和关闭 PHP 标签:
<?php $style = '...'; ?>
以下新的 NewBladeCompiler 将使用 @{ }}
接受所有 PHP 代码,如变量分配、class 声明等
例如@{ $variable = 0; }}
将编译为 <?php $variable=0; ?>
<?php
use Illuminate\View\Compilers\BladeCompiler;
class NewBladeCompiler extends BladeCompiler
{
/**
* Get the echo methods in the proper order for compilation.
*
* @return array
*/
function getEchoMethods()
{
$methods = [
'compileRawEchos' => strlen(stripcslashes($this->rawTags[0])),
'compileEscapedEchos' => strlen(stripcslashes($this->escapedTags[0])),
'compileRegularEchos' => strlen(stripcslashes($this->contentTags[0])),
'compilePhpEchos' => strlen(stripcslashes("@{"))
];
uksort($methods, function ($method1, $method2) use ($methods) {
// Ensure the longest tags are processed first
if($methods[$method1] > $methods[$method2])
{
return -1;
}
if($methods[$method1] < $methods[$method2])
{
return 1;
}
// Otherwise give preference to raw tags (assuming they've overridden)
if($method1 === 'compilePhpEchos')
{
return -1;
}
if($method2 === 'compilePhpEchos')
{
return 1;
}
if($method1 === 'compileRawEchos')
{
return -1;
}
if($method2 === 'compileRawEchos')
{
return 1;
}
if($method1 === 'compileEscapedEchos')
{
return -1;
}
if($method2 === 'compileEscapedEchos')
{
return 1;
}
});
return $methods;
}
function compilePhpEchos( $value )
{
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', "@{", "}}");
$callback = function ($matches) {
$whitespace = empty($matches[3]) ? '' : $matches[3] . $matches[3];
return $matches[1] ? substr($matches[0], 1) : '<?php ' . $matches[2] . ' ?>' . $whitespace;
};
return preg_replace_callback($pattern, $callback, $value);
}
}
?>
Laravel 食谱提出了一种简单但有效的方法,无需包含 PHP 标签:
{{--*/ $var = 'test' /*--}}
{{-- --}} 作为 blade 评论 / 和 / 恢复对
的评论效果<?php $var = 'test' ?>
问题是它比包含 PHP 个标签要长:-(
根据 documentation,在 Laravel 5.2 和更高版本中,您可以使用以下代码:
@php
{{-- PHP code here --}}
@endphp
或者,您可以按照 here 的描述扩展 Blade 模板引擎。
如果以上两种解决方案都不合适,您将无法使用给出的答案
在现代 Laravel (6/7) 你应该这样做:
@php
yourphpcode();
@endphp