Blade模板变量使用集锦

来源:互联网 发布:手游传奇网络加速器 编辑:程序博客网 时间:2024/06/01 07:24

所有的blade模板解析后的文件存在\storage\framework\views目录下

1、读取配置

// 读取config/blog.php中的title属性值{{ config('blog.title') }}

2、分页

调用分页paginate方法返回如下数据

// 可以调用任何分页相关属性LengthAwarePaginator {#160 ▼  #total: 17  #lastPage: 2  #items: Collection {#171 ▶}  #perPage: 10  #currentPage: 1  #path: "http://myblog.com/blog"  #query: []  #fragment: null  #pageName: "page"}// 这里是以函数的方式调用// 当前页{{ $posts->currentPage() }} // 最后一页{{ $posts->lastPage() }}// 前一页<a href="{!! $posts->previousPageUrl() !!}"><a href="{!! $posts->url($posts->currentPage() - 1) !!}">// 后一页<a href="{!! $posts->nextPageUrl() !!}">// 当出现错误时,old方法会读取之前输入的值<input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}">

3、使用自定义函数

// 这里的str_limit调用的是vendor\laravel\framework\src\Illuminate\Support\helpers.php里的方法// 如果content属性不存在的话(也可以指别名),可以通过在Post模型里设置  getContentAttribute(get属性Attribute) 方法返回一个存在的字段public function getContentAttribute($value)    {        return $this->content_raw;    }// 默认100个字符,超过后跟...  {{ str_limit($post->content) }}// url链接<form class="form-horizontal" role="form" method="POST"action="{{ url('/auth/login') }}">

4、循环

// 格式 @foreach ($posts as $post)xxxxxxx@endforeach@foreach ($posts as $post)    <li>        <a href="/blog/{{ $post->slug }}">{{ $post->title }}</a>        <em>({{ $post->publish_at }})</em>        <p>{{ str_limit($post->content) }}</p>    </li>@endforeach

5、日期格式转化
使用日期格式化时,需要先在model中设置dates属性,比如在Post模型类中设置

protected $dates = ['publish_at'];

否则会报如下错误:
Call to a member function format() on string

// May 25, 2016{{ $post->publish_at->format('F j, Y') }}

6、逻辑关系OR

{{ $title or config('blog.title') }}

7、作为参数

// 这里的page_image调用的是自定义的方法<header class="intro-header"          style="background-image: url('{{ page_image($page_image) }}')">

8、调用model里的某个方法

// 这里的$post指的是Model类Post<a href="{{ $post->url($tag) }}">

9、使用PHP自带函数

{!! join(', ', $post->tagLinks()) !!}

10、统计个数

// 这里的post和tags存在一对多的关系$post->tags->count()

11、If使用

@if ($post->tags->count())    {!! join(', ', $post->tagLinks()) !!}@endif// if else使用  <?php if(Auth::guest()): ?>    <li><a href="/auth/login">Login</a></li>  <?php else: ?>    <li class="dropdown">      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"         aria-expanded="false"><?php echo e(Auth::user()->name); ?>        <span class="caret"></span>      </a>      <ul class="dropdown-menu" role="menu">        <li><a href="/auth/logout">Logout</a></li>      </ul>    </li>  <?php endif; ?>

12、三元运算符

{{ $tag ? $tag->tag : '' }}

13、继承模板时传递参数

// 这样在master.blade.php中的{{ $meta_description }}会被替换掉@extends('blog.layouts.master', ['meta_description' => 'Contact Form'])@extends('blog.layouts.master', [  'title' => $post->title,  'meta_description' => $post->meta_description ?: config('blog.description'),])

14、输出错误信息

@if (count($errors) > 0)  <div class="alert alert-danger">    <strong>Whoops!</strong>    There were some problems with your input.<br><br>    <ul>      @foreach ($errors->all() as $error)        <li>{{ $error }}</li>      @endforeach    </ul>  </div>@endif

15、输出成功信息

@if (Session::has('success'))  <div class="alert alert-success">    <button type="button" class="close" data-dismiss="alert">&times;</button>    <strong>      <i class="fa fa-check-circle fa-lg fa-fw"></i> Success. &nbsp;    </strong>    {{ Session::get('success') }}  </div>@endif

16、表单保护

<input type="hidden" name="_token" value="{!! csrf_token() !!}">// 调用edit方法<input type="hidden" name="_method" value="PUT">// 删除方法<input type="hidden" name="_method" value="DELETE">

17、auth认证

  @if (Auth::check())    // 判断输入的url是否匹配 http://larablog.com/admin/post    <li @if (Request::is('admin/post*')) class="active" @endif>      <a href="/admin/post">Posts</a>    </li>    <li @if (Request::is('admin/tag*')) class="active" @endif>      <a href="/admin/tag">Tags</a>    </li>    <li @if (Request::is('admin/upload*')) class="active" @endif>      <a href="/admin/upload">Uploads</a>    </li>  @endif// 获取登陆用户名<?php echo e(Auth::user()->name); ?>// 游客认证<?php if(Auth::guest()): ?>

18、HTML\JS\等里使用

<option @if (in_array($tag, $tags)) selected @endifvalue="{{ $tag }}">

19、生成表单action url

<form class="form-horizontal" role="form" method="POST"action="{{ route('admin.post.update', $id) }}">
0 0
原创粉丝点击