1. if elseif else
路由
<?php
# 常用的标签
Route::prefix('mark')->namespace('my')->group(function () {
# if 语句
Route::get('1', 'Label@mark1');
});
控制器
<?php
namespace App\Http\Controllers\my;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class Label extends Controller
{
// if 标签
public function mark1()
{
$list = [
0 => ['name' => '向上', 'age' => 24, 'hobby' => 'cook'],
1 => ['name' => 'Tom', 'age' => 11, 'hobby' => 'eat'],
2 => ['name' => 'jerry', 'age' => 24, 'hobby' => 'play with tom']
];
return View::exists('myView.mark1') ? view('myView.mark1')->with('list', $list) : view('myView.404');
}
}
模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>if 标签</title>
</head>
<body>
@if (count($list) === 1)
我有一条记录!
@elseif (count($list) > 1)
我有好多条记录!
@else
我没有记录!
@endif
</body>
</html>
运行结果
2. switch case
路由
<?php
# 常用的标签
Route::prefix('mark')->namespace('my')->group(function () {
# switch 语句
Route::get('2', 'Label@mark2');
});
控制器
<?php
namespace App\Http\Controllers\my;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class Label extends Controller
{
// switch 标签
public function mark2()
{
return View::exists('myView.mark2') ? view('myView.mark2') : view('myView.404');
}
}
模板文件
@switch(1)
@case(1)
First case...
@break
@case(2)
Second case...
@break
@default
Default case...
@endswitch
页面显示
3. 循环语句
例子
# 常用的标签
Route::prefix('mark')->namespace('my')->group(function () {
# 循环语句
Route::get('3', 'Label@mark3');
});
页面显示
<?php
namespace App\Http\Controllers\my;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class Label extends Controller
{
// 循环标签
public function mark3()
{
$users = [
['name' => '向上', 'age' => 24, 'hobby' => 'cook'],
['name' => 'Tom', 'age' => 11, 'hobby' => 'eat'],
['name' => 'jerry', 'age' => 24, 'hobby' => 'play with tom']
];
return View::exists('myView.mark3') ? view('myView.mark3', )->with('users', $users) : view('myView.404');
}
}
模板文件
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }} <br>
@endfor
@foreach ($users as $user)
<p>name:{{ $user['name'] }} || age:{{ $user['age'] }} || hobby:{{ $user['hobby'] }}</p>
@endforeach
@while (false)
<p>I'm looping forever.</p>
@endwhile
页面效果