Blade模板变量使用集锦

所有的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 }}">{
   { $
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值