Laravel 5 Blade 模板中的 PHP 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31087937/
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
PHP code inside Laravel 5 Blade Template
提问by abu abu
I have to place some PHP code inside Laravel 5 Blade Template. Like below
我必须在 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
Which is the actual procedure to place PHP code inside Laravel 5 Blade Template ??
将 PHP 代码放入 Laravel 5 Blade 模板的实际过程是什么?
回答by user28864
According to documentation, in Laravel 5.2 and newer you can use the following code:
根据文档,在 Laravel 5.2 及更新版本中,您可以使用以下代码:
@php
{{-- php code here --}}
@endphp
Alternatively, you can extend the Blade templating engine as it's described here.
另外,因为它的描述可以延长刀片模板引擎这里。
If neither of the above solutions is suitable, you are stuck with the answers given by @Armen and @Gonzalo
如果上述解决方案都不合适,您将被@Armen 和@Gonzalo 给出的答案所困扰
回答by Armen Markossyan
Just open and close php tags: <?php $style = '...'; ?>
只需打开和关闭 php 标签: <?php $style = '...'; ?>
回答by Gonzalo Cao
Laravel recipes suggest a simple but effective way to do it without including the php tags
Laravel recipes 提出了一种简单但有效的方法来做到这一点,而无需包含 php 标签
{{--*/ $var = 'test' /*--}}
{{-- --}} works as a blade comment / and /reverts the effect of comment resulting on
{{-- --}} 用作刀片注释/ 和 /恢复注释的效果
<?php $var = 'test' ?>
The problem is that is longer than including php tags :-(
问题是这比包含 php 标签要长:-(
回答by Elitexp
The following new NewBladeCompiler will use @{ }}
for accepting all php codes like variable assigning, class declaration etc.
e.g. @{ $variable = 0; }}
will be compiled to <?php $variable=0; ?>
以下新的 NewBladeCompiler 将@{ }}
用于接受所有 php 代码,如变量赋值、类声明等。例如@{ $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);
}
}
?>