douzhenqun1271
2016-12-22 00:25
浏览 61
已采纳

如何在刀片中设置变量(laravel 5.3)?

I try like this :

...

    @if(is_null($key['p3']))
        {{--*/$p3 = $key['p3']/*--}}
    @else
        {{ 0 }}
    @endif
    @if(is_null($key['wabku']))
        {{--*/$wabku = $key['wabku']/*--}}
    @else
        {{ 0 }}
    @endif
    <td class="tg-rv4w" width="5%">
        {{ number_format($p3 - $wabku,0,',','.') }}
    </td>

...

But, It's not working.

Is there any people who can help me?

图片转代码服务由CSDN问答提供 功能建议

我尝试这样:

  ... 
  
 @if(is_null($ key ['p3']))
 {{ -  * / $ p3 = $ key ['p3'] / *  - }} 
 @else 
 {{0}  } 
 @endif 
 @if(is_null($ key ['wabku']))
 {{ -  * / $ wabku = $ key ['wabku'] / *  - }} 
 @else \  n {{0}} 
 @endif 
&lt; td class =“tg-rv4w”width =“5%”&gt; 
 {{number_format($ p3  -  $ wabku,0,',','。  '}}} 
&lt; / td&gt; 
 
 ... 
   
 
 

但是,它不起作用。 < 有没有人可以帮助我?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtun1872 2016-12-26 05:34
    已采纳

    You are using laravel-4 template comment syntax to define/set variables which is may be not working with L5.x.

    But you can try @php ($p3 = $key['p3'])

    OR

    @php
    $p3 = $key['p3']
    @endphp
    

    Above both are same.

    Further you go with create own service provider like 1 create BladeServiceProvider:

    <?php 
    //app/Providers/BladeServiceProvider.php
    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    class BladeServiceProvider extends ServiceProvider
    {
        public function boot()
        {
            /* @datetime($var) */
            \Blade::extend(function($view, $compiler)
            {
                $pattern = $compiler->createOpenMatcher('datetime');
    
                return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\')); ?>', $view);
            });
    
            /* @eval($var++) */
            \Blade::extend(function($view)
            {
                return preg_replace('/\@eval\((.+)\)/', '<?php ${1}; ?>', $view);
            });
        }
    
        public function register()
        {
            //
        }
    }
    

    2 Register BladeServiceProvider:

    <?php
    //in config/app.php add
    return [
    
        // ...
    
        'providers' => [
    
            // ...
    
            'App\Providers\BladeServiceProvider',
    

    Clear complied artisan clear-compiled Assign value to variable @datetime($updated_at)

    OR

    @eval($var = 1)
    

    Taken reference from: Laravel 5 alternative

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题