1. if elseif else

路由

  1. <?php
  2. # 常用的标签
  3. Route::prefix('mark')->namespace('my')->group(function () {
  4. # if 语句
  5. Route::get('1', 'Label@mark1');
  6. });

控制器

  1. <?php
  2. namespace App\Http\Controllers\my;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\View;
  6. class Label extends Controller
  7. {
  8. // if 标签
  9. public function mark1()
  10. {
  11. $list = [
  12. 0 => ['name' => '向上', 'age' => 24, 'hobby' => 'cook'],
  13. 1 => ['name' => 'Tom', 'age' => 11, 'hobby' => 'eat'],
  14. 2 => ['name' => 'jerry', 'age' => 24, 'hobby' => 'play with tom']
  15. ];
  16. return View::exists('myView.mark1') ? view('myView.mark1')->with('list', $list) : view('myView.404');
  17. }
  18. }

模板文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>if 标签</title>
  8. </head>
  9. <body>
  10. @if (count($list) === 1)
  11. 我有一条记录!
  12. @elseif (count($list) > 1)
  13. 我有好多条记录!
  14. @else
  15. 我没有记录!
  16. @endif
  17. </body>
  18. </html>

运行结果
image.png


2. switch case

路由

  1. <?php
  2. # 常用的标签
  3. Route::prefix('mark')->namespace('my')->group(function () {
  4. # switch 语句
  5. Route::get('2', 'Label@mark2');
  6. });

控制器

  1. <?php
  2. namespace App\Http\Controllers\my;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\View;
  6. class Label extends Controller
  7. {
  8. // switch 标签
  9. public function mark2()
  10. {
  11. return View::exists('myView.mark2') ? view('myView.mark2') : view('myView.404');
  12. }
  13. }

模板文件

  1. @switch(1)
  2. @case(1)
  3. First case...
  4. @break
  5. @case(2)
  6. Second case...
  7. @break
  8. @default
  9. Default case...
  10. @endswitch

页面显示
image.png


3. 循环语句

例子

  1. # 常用的标签
  2. Route::prefix('mark')->namespace('my')->group(function () {
  3. # 循环语句
  4. Route::get('3', 'Label@mark3');
  5. });

页面显示

  1. <?php
  2. namespace App\Http\Controllers\my;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\View;
  6. class Label extends Controller
  7. {
  8. // 循环标签
  9. public function mark3()
  10. {
  11. $users = [
  12. ['name' => '向上', 'age' => 24, 'hobby' => 'cook'],
  13. ['name' => 'Tom', 'age' => 11, 'hobby' => 'eat'],
  14. ['name' => 'jerry', 'age' => 24, 'hobby' => 'play with tom']
  15. ];
  16. return View::exists('myView.mark3') ? view('myView.mark3', )->with('users', $users) : view('myView.404');
  17. }
  18. }

模板文件

  1. @for ($i = 0; $i < 10; $i++)
  2. The current value is {{ $i }} <br>
  3. @endfor
  4. @foreach ($users as $user)
  5. <p>name:{{ $user['name'] }} || age:{{ $user['age'] }} || hobby:{{ $user['hobby'] }}</p>
  6. @endforeach
  7. @while (false)
  8. <p>I'm looping forever.</p>
  9. @endwhile

页面效果
image.png