美文网首页
Blade 模板引擎流程控制

Blade 模板引擎流程控制

作者: 周怡 | 来源:发表于2018-02-16 08:26 被阅读0次

    ViewController 里的数据

    class ViewController extends Controller
    {
        public function index()
        {
            
            $data = [
                'score' => 50,
                'num' => 10,
                'article' => [
                    'news1',
                    'news2',
                    'news3',
                    'news4',
                    'news5',
                ],
                'news' => [],
            ];
            return view('my_laravel', ['data' => $data]);
        }
    }
    

    .blade.php 代码

    1 @if

    @if ($data['score'] < 60)
                            failed
                       @else
                            pass
                       @endif
    

    2 @unless

     @unless ($data['score'] > 60)
                            Sorry, you failed.
                       @endunless
    

    3 @for

    @for ($i = 0; $i <= $data['num']; $i++)
                            {{$i}}
                       @endfor
    

    4 @foreach and @forelse

    @foreach ($data['article'] as $v)
                            {{$v}}
                       @endforeach
                       </br>
                       @forelse ($data['news'] as $v)
                            {{$v}}
                            @empty
                                no data;
                       @endforelse
    

    5 嵌套

    @foreach ($data['article'] as $k => $v)
                            @if ($k > 2)
                                {{$k}} => {{$v}}
                            @endif
                       @endforeach
    

    相关文章

      网友评论

          本文标题:Blade 模板引擎流程控制

          本文链接:https://www.haomeiwen.com/subject/nrtptftx.html