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?

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?