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">×</button> <strong> <i class="fa fa-check-circle fa-lg fa-fw"></i> Success. </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
- Blade模板变量使用集锦
- Blade模板变量使用集锦
- laravel blade模板loop变量使用
- blade模板使用
- blade模板的使用
- blade模板使用总结1
- laravel视图blade模板使用集合
- Laravel基础-blade模板引擎之变量操作
- Blade模板命令集
- Laravel blade 模板积累
- Blade模板引擎
- Laravel blade 模板积累
- 13. Blade 模板引擎
- Laravel-Blade模板引擎
- Laravel Blade模板引擎
- blade模板引擎模仿
- laravel blade模板
- Laravel 5.3 新特性系列 —— Blade 模板新增 $loop 变量
- 详解rotate3()
- SQL Server查询所有数据库,一个数据库中所有表和表的所有字段
- 音调
- Android CheckBox的OnCheckedChangeListener监听传值
- Gas Station
- Blade模板变量使用集锦
- webstorm卡在scanning files to index
- 谱估计
- Android基础
- SQLSERVER DBCC 用法说明
- HDU 5696 区间的价值
- 标日初上-第1課
- css 优先级总结
- iOS新特性之实现3D Touch