【Laravel5.3 笔记整理七】Laravel视图(一)

1. 其实就是 HTML页面,把控制器中分配的数据进行处理和展示


2. 使用(只分配一个数据)

以下为实例:

1) 路由中 web.php
Route::get('shitu', "ShiTuController@index");
2) 控制器中 app/Http/Controllers/ShiTuController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ShiTuController extends Controller
{
    public function index()
    {
        $name = "选择自己所爱的,爱自己所选择的!";
        // 只分配一个数据
        return view('shitu')->with('qianming', $name); // 视图页面中的输出变量与此处的一致 $qianming,否则无法输出
    }
}
3) 视图中 resources/views/shitu.blade.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视图</title>
</head>
<body>
    <!-- 页面可以直接使用PHP代码【但是不推荐使用】-->
    <?php
        echo $qianming;
    ?>

    <!-- 【通常使用该种格式】(五星推荐)-->
    {{$qianming}}  
</body>
</html>

运行结果:

laravel视图


3. 定界符

{{}}
smarty 模板解析的原理
{{$qianming}} 通过一些替换技术(正则匹配) 最终替换成 <?php echo $qianming; ?>


4. 分配多个数据

在 ShiTuController.php 中,分配多个数据 可以使用三种方法:

① 该方法不常用

public function index()
{
    $name = "选择自己所爱的,爱自己所选择的!";
    $nametwo = "让学习成为一种习惯!";
    return view('shitu')->with('name', $name)->with('nametwo', $nametwo);
} 

② 该方法常用,但是不是最简便的(两个或者三个可以用该方法)

public function index()
{
    $name = "选择自己所爱的,爱自己所选择的!";
    $nametwo = "让学习成为一种习惯!";
    return view('shitu')->with(['name'=>$name, 'nametwo'=>$nametwo]);
}

③ 该方法最常用【店长五星推荐】(两个以上推荐该方法)

public function index()
{
    $name = "选择自己所爱的,爱自己所选择的!";
    $nametwo = "让学习成为一种习惯!";

    $data = array(
        'name' => $name,
        'nametwo' => $nametwo
    );

    return view('shitu')->with($data);
}

以上三种写法的执行结果是一致的


以下为实例:

1) 路由中 web.php
Route::get('shitu', "ShiTuController@index");
2) 控制器中 app/Http/Controllers/ShiTuController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ShiTuController extends Controller
{
    public function index()
    {
        $name = "选择自己所爱的,爱自己所选择的!";
        $nametwo = "让学习成为一种习惯!";
        // 分配多个数据
        return view('shitu')->with('name', $name)->with('nametwo', $nametwo);  
    }
}
3) 视图中 resources/views/shitu.blade.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视图</title>
</head>
<body>
    {{$name}}
    {{$nametwo}}
</body>
</html>

运行结果:

laravel视图-分配多个数据


5. 模板中使用函数

1) 可以单独使用函数
// 将时间格式化
{{data('Y-m-d H:i:s')}}
2) 可以结合传过来的参数,嵌套使用
// 将传过来的密码,进行md5加密
{{md5($pass)}}

// 将密码加密,并转为大写
{{strtoUpper(md5($pass))}}

// 将密码加密,并转为大写,且从第10个开始,截取2位
{{substr(strtoUpper(md5($pass)), 10, 2)}}

实例:

  • web.php
Route::get('shitu', "ShiTuController@index");
  • app / Http / Controllers / ShiTuController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Request;

class ShiTuController extends Controller
{
    public function index()
    {
        // 使用函数
        return view('shitu')->with('pass', '123'); // 将密码为123,传入页面
    }
}
  • resources / views / shitu.blade.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视图</title>
</head>
<body>
    {{-- 将时间格式化 --}}
    {{date('Y-m-d H:i:s')}}
    <hr>

    {{-- 将传过来的密码进行加密 --}}
    {{md5($pass)}}
    <hr>

    {{-- 将密码加密,并转为大写 --}}
    {{strtoUpper(md5($pass))}}
    <hr>

    {{-- 将密码加密,并转为大写,且从第10个开始,截取2个 --}}
    {{substr(strtoUpper(md5($pass)), 10, 2)}}

</body>
</html>

运行结果:

laravel模板中使用函数


6. 判断值是否存在

​ 存在输出对应的值;不存在输出默认值

// 判断 $pass1 是否存在,存在输出对应的值;不存在数据默认值
{{$pass1 or "数据不存在"}}

实例:

  • web.php
Route::get('shitu', "ShiTuController@index");
  • app / Http / Controllers / ShiTuController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ShiTuController extends Controller
{
    public function index()
    {
        return view('shitu')->with('pass', '123')->with('pass1', '234');
    }
}
  • resources / views / shitu.blade.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视图</title>
</head>
<body>
     {{-- 将时间格式化 --}}
    {{date('Y-m-d H:i:s')}}
    <hr>

    {{-- 将传过来的密码进行加密 --}}
    {{md5($pass)}}
    <hr>

    {{-- 将密码加密,并转为大写 --}}
    {{strtoUpper(md5($pass))}}
    <hr>

    {{-- 将密码加密,并转为大写,且从第10个开始,截取2个 --}}
    {{substr(strtoUpper(md5($pass)), 10, 2)}}

    {{-- 判断 $pass1 是否存在,存在输出对应数据;不存在使用默认值 --}}
    {{$pass1 or "数据不存在"}}
</body>
</html>

运行结果:

laravel判断值是否存在


7. 输出HTML代码

1) ShiTuController.php
return view('shitu')->with('html', '<h2>This is a test</h2>');
2) shitu.blade.php
{!!$html!!}

实例:

* web.php

Route::get('shitu', "ShiTuController@index");

* app / Http / Controllers / ShiTuController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ShiTuController extends Controller
{
    public function index()
    {
        return view('shitu')->with('html', '<h2>This is a test</h2>');
    }
}
  • resources / views / shitu.blade.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视图</title>
</head>
<body>
    <?php
//        echo $qianming;
    ?>

    {{--{{$qianming}}--}}

    {{--{{$name}}--}}
    {{--{{$nametwo}}--}}

    {{-- 将时间格式化 --}}
    {{date('Y-m-d H:i:s')}}
    <hr>

    {{-- 将传过来的密码进行加密 --}}
    {{md5($pass)}}
    <hr>

    {{-- 将密码加密,并转为大写 --}}
    {{strtoUpper(md5($pass))}}
    <hr>

    {{-- 将密码加密,并转为大写,且从第10个开始,截取2个 --}}
    {{substr(strtoUpper(md5($pass)), 10, 2)}}
    <hr>

    {{-- 判断 $pass1 是否存在,存在输出对应数据;不存在使用默认值 --}}
    {{$pass1 or "数据不存在"}}
    <hr>

    {{-- 在页面输出HTML代码 --}}
    {{!!$html!!}}

</body>
</html>

运行结果:

laravel视图输出HTML代码


若将 shitu.blade.php 中 写成一下格式,将不会展示效果:

{{$html}} // 此写法为默认控制器中分配的HTML代码,会直接将其转换为实体

运行结果:

laravel视图输出HTML格式代码


8. 直接输出实体

​ 即不解析 {{}}

@{{$pass}}

实例:

  • web.php
Route::get('shitu', "ShiTuController@index");
  • app / Http / Controllers / ShiTuController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ShiTuController extends Controller
{
    public function index()
    {
        return view('shitu')->with('pass', '123')->with('pass1', '234')->with('html', '<h2>This is a test</h2>');
    }
}
  • resources / views / shitu.blade.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视图</title>
</head>
<body>
    <?php
//        echo $qianming;
    ?>

    {{--{{$qianming}}--}}

    {{--{{$name}}--}}
    {{--{{$nametwo}}--}}

    {{-- 将时间格式化 --}}
    {{date('Y-m-d H:i:s')}}
    <hr>

    {{-- 将传过来的密码进行加密 --}}
    {{md5($pass)}}
    <hr>

    {{-- 将密码加密,并转为大写 --}}
    {{strtoUpper(md5($pass))}}
    <hr>

    {{-- 将密码加密,并转为大写,且从第10个开始,截取2个 --}}
    {{substr(strtoUpper(md5($pass)), 10, 2)}}
    <hr>

    {{-- 判断 $pass1 是否存在,存在输出对应数据;不存在使用默认值 --}}
    {{$pass1 or "数据不存在"}}
    <hr>

    {{-- 在页面输出HTML代码 --}}
    {!!$html!!}
    <hr>

    {{-- 以实体形式输入,不让其解析 {{}} --}}
    @{{$pass}}

</body>
</html>

运行结果:

laravel视图-以视图的形式输出

猜你喜欢

转载自blog.csdn.net/studyphp123/article/details/81871366