简体   繁体   中英

@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.

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:

/*
|--------------------------------------------------------------------------
| 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

@define $i = 1  

try something like this;

@php 

$i=1;

@endphp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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