登录  /  注册
博主信息
博文 77
粉丝 0
评论 0
访问量 76187
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
laravel框架基础_1105(blade模板的语法练习)
Jet的博客
原创
1054人浏览过

1、练习一下blade模板的语法。如:变量渲染、foreach、if...else...
2、include、section

一、laravel框架提供了blade模板相关的语法,文件名命名为:xxx.blade.php

    此后,可使用相关语法,如:

    @foreach

    @endforeach

    引用变量: {{变量名}}等

二、include加载方法,对上一个作业也有说过

    文件名命名为blade的,可以直接使用:@include('admins.public.header')

    此时实际上加载路径为:admins/public/header.blade.php文件

三、section方法:模板层级

    语法:

    母版:section('内容') 要与子版z中的内容一致

    @section('workplace')

    

    @show

    子版:需要继承母版【语法:@extends('layout.index')  //继承 layout/index.blade.php文件 】

    @section('workplace')

        @parent    //保留母版内容

        <div>新闻列表</div>

    @endsection

    此时,母版会显示子版<div>新闻列表</div>内容,在保留了母版内容后面显示

四、yield方法,也是模板层级

    母版:

    @yield('content')

    子版:

    @section('content')

        <div> good good study, day day up </div>

    @endsection

    此时母版会显示字母<div> good good study, day day up </div>内容。

    其实,相当于赋值意思。


路由文件:

Route::get('/admins/home/index','admins\Home@index');

控制器文件:

<?phpnamespace App\Http\Controllers\admins;use Illuminate\Http\Request;
use App\Http\Controllers\Controller;class Home extends Controller
{
    public function index(){
     $data['navs'] = array(
      array('title'=>'企业logo','url'=>'/','target'=>'_blank'),
      array('title'=>'新闻资讯','url'=>'https://news.163.com','target'=>'_blank'),
      array('title'=>'公司产品','url'=>'http://www.php.cn','target'=>'_blank')
     );     $data['span'] = '<span style="color:red">红色</span>';     $data['site_title'] = 'PHP中文网';     //return view('admins/home/index',$data);
     return view('home',$data);
    }    public function lists(){
     $data['navs'] = array(
      array('title'=>'企业logo','url'=>'/','target'=>'_blank'),
      array('title'=>'新闻资讯','url'=>'https://news.163.com','target'=>'_blank'),
      array('title'=>'公司产品','url'=>'http://www.php.cn','target'=>'_blank')
     );     $data['span'] = '<span style="color:red">红色</span>';     $data['site_title'] = 'PHP中文网';
     return view('lists',$data);
    }
}

视图文件:

<!DOCTYPE html>
<html>
<head>
 <title>{{$site_title}}</title>
 <style type="text/css">
  body{padding:0;margin: 0;}
  .header{background: #000;width:100%;height: 50px; text-align: center;line-height: 50px;}
  .header a{color:#fff;margin: 0px 30px; text-decoration: none;}
  .nav-left{width: 15%; height: 500px; background: red;float: left;}
  .news{width:85%;height: 500px;background: green;float: left;}
  .clear{clear:both;}  .footer{background: #000;width:100%;height: 50px; text-align: center;line-height: 50px;}
  .footer a{color:#fff;margin: 0px 30px; text-decoration: none;}
 </style>
</head>
<body>
 <!---->
 <div class="header">
  @foreach($navs as $nav)
  <a href="{{$nav['url']}}" target="{{$nav['target']}}">{{$nav['title']}}</a>
  @endforeach
 </div> <!---->
 <div class="nav-left">
  @section('workplace')
   <span style="color:red">红色</span>
  @show
 </div>
 <!---->
 <div class="news">
  <div>container</div>
  @yield('content')
 </div>
 <div class="clear"></div>
 <!---->
 <div class="footer">
  <a href="#">php中文网</a>
  <a href="http://www.baidu.com">百度</a>
  <a href="http://www.php.cn">中文网</a>
 </div>
</body>
</html>

视图文件,子版:

lists.blade.php

@extends('layout.index')@section('workplace')
 <div>新闻列表</div>
@endsection

home.blade.php

@extends('layout.index')@section('workplace')
 <div>网站首页</div>
@endsection@section('content')
 <div>good good study, day day up </div>
@endsection

TIM截图20191108235656.jpg


总结:

使用了blade模板命名,可以使用它的相关语法,@foreach、{{$变量}}、{!!$变量!!} 语法转义、继承母版,模板层级使用方法。



批改状态:合格

老师批语:laravel模板比较简洁强大
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学