Laravel 5.6 視圖用Blade語法傳遞變量和流程控制if 語句和循環語句

Laravel5.6 視圖用Blade語法傳遞變量和流程控制if 語句和循環語句php

Laravel 的 View 部分是內置了兩套輸出系統:直接輸出和使用 Blade 引擎「編譯」後輸出,默認狀況下它們經過文件名後綴來選擇:.blade.php 後綴的認爲是模板視圖文件,其餘的 .php 文件按照 PHP 自己的方式執行。雖然 Blade 模板文件中也能夠隨意嵌入 PHP 代碼,但若是並無使用,系統還去進行語法解析和替換也是沒有必要的,這樣能夠提升效率。css

Laravel使用的是Blade視圖引擎
Blade視圖文件使用.blade.php
傳遞:變量到視圖
顯示:經過兩個{{}}花括號包裹起來來顯示html

能夠使用@if @elseif @else @endif 來構造if語句
這些指令的功能和php對應的功能同樣
switch語句和for語句jquery

web.phpweb

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/helloworld',function(){
	//return "Hello World! 這個是不使用模板 自定義字符串";
	return view('helloworld',['name'=>'Laravel Zero Basic Quickstart','title'=>'This is Title','records'=>0]);
}
);

Route::get('/bootstrap',function(){
	return view('bootstrap');
});

  helloworld.blade.phpbootstrap

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>{{$title}}</title>
</head>
<body>
<div class="jumbotron text-center">
<h1>{{$name}}</h1>
<p>Video Course</p>
</div>
<div class="container">

<div class="row">
<div class="col-sm-8">
    <div class="card">
        <div class="card-header">Course List</div>
        <div class="card-body">Content
        @if($records ===1)
            I have one records
        @elseif($records>1)
            I have multiple records
        @else
            I don't have any records
        @endif

        <br/>
        @for($i=0;$i<10;$i++)
            The current value is {{$i}}<br/>
        @endfor
        </div>
    </div>
</div>

<div class="col-sm-4">
    <div class="card-header">Course Intro</div>
    <div class="card-body">Laravel Quickstart</div>
    <div class="card-footer">Footer</div>
</div>

</div>

</div><!--END container-->

<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
相關文章
相關標籤/搜索