Blade 模板引擎流程控制

2018-02-16  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读