@define 变量在 Blade 中不起作用 Laravel 4.2
@define variable not working in Blade Laravel 4.2
尝试在 blade 文件中定义一个变量,但它不起作用:
@define $i = 1
但尝试使用时出现错误 "Undefined variable $i"。
完整代码:
@foreach($assigned as $task)
@define $pcat = "";
@if($task->tc_name != $pcat)
@else
//code
@endif
@define $pcat = $task->tc_name
@endforeach
不知道我哪里做错了:(
你需要像
一样扩展blade
像这样扩展 Blade:
/*
|--------------------------------------------------------------------------
| Extend blade so we can define a variable
| <code>
| @define $variable = "whatever"
| </code>
|--------------------------------------------------------------------------
*/
\Blade::extend(function($value) {
return preg_replace('/\@define(.+)/', '<?php ; ?>', $value);
});
你可以把上面的代码放在 app/start/global.php 的底部(或者你觉得更好的任何其他地方)然后使用
@define $i = 1
尝试这样的事情;
@php
$i=1;
@endphp
我想你可以试试这个:
<?php $i = 1; ?>
{{$i}}
或者您可以在 blade 文件中使用以下代码
{{--*/$i=1/*--}}
{{$i}}
希望这对你有用!
尝试在 blade 文件中定义一个变量,但它不起作用:
@define $i = 1
但尝试使用时出现错误 "Undefined variable $i"。
完整代码:
@foreach($assigned as $task)
@define $pcat = "";
@if($task->tc_name != $pcat)
@else
//code
@endif
@define $pcat = $task->tc_name
@endforeach
不知道我哪里做错了:(
你需要像
一样扩展blade像这样扩展 Blade:
/*
|--------------------------------------------------------------------------
| Extend blade so we can define a variable
| <code>
| @define $variable = "whatever"
| </code>
|--------------------------------------------------------------------------
*/
\Blade::extend(function($value) {
return preg_replace('/\@define(.+)/', '<?php ; ?>', $value);
});
你可以把上面的代码放在 app/start/global.php 的底部(或者你觉得更好的任何其他地方)然后使用
@define $i = 1
尝试这样的事情;
@php
$i=1;
@endphp
我想你可以试试这个:
<?php $i = 1; ?>
{{$i}}
或者您可以在 blade 文件中使用以下代码
{{--*/$i=1/*--}}
{{$i}}
希望这对你有用!