[Laravel] LAMP+Laravel 學習筆記-view-blade樣板

laravel 的view層預設使用blade樣板
重要指令:  return view;
                 {{message}}
                 layout

  1. 
    
    #routing 直接定義,路徑:/routes/web.php
    Route::get('product', function () {
        return view('product', ['caregoryName' => '3C-PC-Apple']);
    });
    
    #controller回傳view,路徑:controller/product.php
    
    class product extends Controller
    {  
        public function index($categoryName)
        {        
            return view('product',array('categoryName'=>$categoryName));
        }     
    }
    
    
    #view的命名規則:view名稱.blade.php ,路徑: /resources/views/product.blade.php
    <h2 class="page_title">
                    <span class="title_line"></span> {{$categoryName}} <span class="title_line"></span>
    </h2>
    
    

layout
@include 為需要include的header or footer
@section 為要此layout的子類別需定義

#layout file:/resources/views/layouts/main.blade.php

<!DOCTYPE html>
<html>
@include('include.header')

<body id="top">
    @include('include.navbar')

    @yield('content')

    @include('include.footer')
</body>

</html>
  • 套用layout的版型
#file:/resources/views/product.blade.php

@extends('layouts.main')
@section('content')
  this is 子模板:product
@stop
  • @yield 與@section @show/@stop的關係
#layout
@yield('content')

#template file
@section('content')
  內文,需靠yield('content')來顯示
@stop  #stop等於endsection




layout:
template
@section('content')
   直接顯示內文
@show


foreach的使用

#controller 傳入商品物件
       $data['categoryName']=$categoryName;
        $product=new \stdClass();
        $product->productName="iPhone6S";
        $product->price=20000;
        $product->salePrice=12000;
        $product->productUrl="/productdetail/1160";
        $product->imgUrl="/img/pro1.jpg";        
        $products[]=$product;

        $product->productName="琉元燕盞200g禮盒";
        $product->price=24000;
        $product->salePrice=16000;
        $product->productUrl="/productdetail/1186";
        $product->imgUrl="/img/pro2.jpg";        
        $products[]=$product;
        
        $data['products']=$products;        
        return view('product',$data);           


      
#view 使用foreach 顯示

            @foreach ($products as $product)
            <div class="col-xs-12 col-sm-3 index_product_list">
                <a href="/productdetail/1160">
                    <span class="pro_img">
                        <img class="img-responsive zoom" src="/img/pro1.jpg">
                    </span>
                    <span class="pro_name">{{$product->productName}}</span>
                    <span class="pro_priceO">原價 {{$product->price}}</span>
                    <span class="pro_price">特價<b><i>$</i>{{$product->salePrice}}</b></span>
                </a>
            </div>
             @endforeach



參考:官方文件Blade Templates