1. 程式人生 > >laravel怎麼做模板的頭尾分離

laravel怎麼做模板的頭尾分離

關注博主,每天分享專案實戰經驗

1.首先找到頁面的公共部分,放在layouts這個目錄下的home.blade.php

2.設定內容區域

@yield('content')

3.在其他頁面使用模板引擎,先繼承,然後在使用section即可

@extends('layouts.home')

@section('content')
	<div class="login">
		<div class="container">
		<form action="" method="post">
			<div class="col-md-6 login-do1 animated wow fadeInLeft" data-wow-delay=".5s">
				<div class="login-mail">
					<input type="text" name="email" placeholder="Email" required="">
					<i class="glyphicon glyphicon-envelope"></i>
				</div>
				<div class="login-mail">
					<input type="password" name="password" placeholder="Password" required="">
					<i class="glyphicon glyphicon-lock"></i>
				</div>
				<div class="login-mail">
					<input type="password" name="repeate" placeholder="Repeated password" required="">
					<i class="glyphicon glyphicon-lock"></i>
				</div>
				  <a class="news-letter" href="#">
						 <label class="checkbox1"><input type="checkbox" name="checkbox" ><i> </i>I agree with the terms</label>
					   </a>
	
			</div>
			<div class="col-md-6 login-do animated wow fadeInRight" data-wow-delay=".5s">
				<label class="hvr-sweep-to-top login-sub">
					<input type="submit" value="Submit">
					</label>
					<p>Already register</p>
				<a href="login.html" class="hvr-sweep-to-top">Login</a>
			</div>
			<div class="clearfix"> </div>
			<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
			</form>
		</div>


	</div>
@endsection

4.在這裡其實還有一個問題,那就是檔案的title這個值,這個值我們需要怎麼做呢!其實也很簡單

將下邊的這程式碼放置在一個公共的控制器,其他的控制器繼承這個控制器,在每次載入頁面的時候,呼叫這個方法即可

名為HomeController.php

public function set_page_info($title, $keywords, $description, $showNav, $css=array(), $js=array()){
    return array(
        'page_title'            => $title,
        'page_keywords'         => $keywords,
        'page_description'      => $description,
    );

IndexController.php

/*
author:咔咔
address:陝西西安
wechat:fangkangfk
*/


class IndexController extends HomeController
{

	/**
	 * @return $this
	 */
	public function index(Request $request){
			//設定頁面資訊
			$data = $this->set_page_info('京西商城','京西','京西');
    }

}