view() 函数
1.view() 函数的第二个参数可穿变量,可以以数组的形式一次传多个变量:
class ViewController extends Controller
{
public function index()
{
$name = 'zhouyi';
$age = 25;
return view('my_laravel', ['name' => $name, 'age' => $age]);
}
}
- 变量存在与否的两种处理方式
- PHP 原生处理方法
<div class="title m-b-md">
<?= isset($name)?$name:'XXX' ?>
{{$name}} <!-- use @ to cover -->
<?= $age ?> is not true!
</div>
- .blade 处理方法
<div class="title m-b-md">
{{isset($name)?$name:'aaa'}} <!-- use @ to cover -->
<?= $age ?> is not true!
</div>
3.解析js代码
- 定义变量str
public function index()
{
$str = '<script>document.write("My Laravel")</script>';
return view('my_laravel', ['str' => $str]);
}
- 解析str
<div class="title m-b-md">
{!!$str!!}
</div>
网友评论