簡體   English   中英

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

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

嘗試在刀片文件中定義變量,但不起作用:

@define $i = 1  

但嘗試使用時出現錯誤“未定義變量$ i”。

完整代碼:

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

不知道我在哪里做錯了:(

我認為您可以嘗試以下方法:

<?php $i = 1; ?>

{{$i}}

或者,您可以在刀片文件中使用以下代碼

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

{{$i}}

希望這項工作對您有幫助!

您需要像

像這樣擴展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);
});

使用完之后,您可以將上面的代碼放在app / start / global.php的底部(如果感覺更好,可以放在其他任何地方)

@define $i = 1  

嘗試這樣的事情;

@php 

$i=1;

@endphp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM