简体   繁体   English

@define变量在Blade Laravel 4.2中不起作用

[英]@define variable not working in Blade Laravel 4.2

Trying to define a variable in blade file but its not working: 尝试在刀片文件中定义变量,但不起作用:

@define $i = 1  

but got error "Undefined variable $i" when try to use. 但尝试使用时出现错误“未定义变量$ i”。

Full Code: 完整代码:

@foreach($assigned as $task)  
  @define $pcat = "";
  @if($task->tc_name != $pcat)
  @else
       //code
  @endif
  @define $pcat = $task->tc_name
@endforeach

Dont know where I'm doing wrong :( 不知道我在哪里做错了:(

I think you can try this: 我认为您可以尝试以下方法:

<?php $i = 1; ?>

{{$i}}

OR you can use below code in your blade file 或者,您可以在刀片文件中使用以下代码

{{--*/$i=1/*--}}

{{$i}}

Hope this work for you ! 希望这项工作对您有帮助!

You need to extend blade like 您需要像

Extend Blade like this: 像这样扩展Blade:

/*
|--------------------------------------------------------------------------
| Extend blade so we can define a variable
| <code>
| @define $variable = "whatever"
| </code>
|--------------------------------------------------------------------------
*/

\Blade::extend(function($value) {
    return preg_replace('/\@define(.+)/', '<?php ${1}; ?>', $value);
});

You can just put the above code on the bottom of app/start/global.php (or any other place if you feel that is better) after that use 使用完之后,您可以将上面的代码放在app / start / global.php的底部(如果感觉更好,可以放在其他任何地方)

@define $i = 1  

try something like this; 尝试这样的事情;

@php 

$i=1;

@endphp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM