crhayes/blade-partials 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

crhayes/blade-partials

Composer 安装命令:

composer require crhayes/blade-partials

包简介

README 文档

README

This package introduces the concept of partials (not includes) in blade templating. Think of it like inline @extends functionality within your views.

Partials allow you to extend a view and inject content into it, inline, within your views.

Why?

To make it easier to create reusable components. I often find myself repeating a lot of HTML boilerplate when using frameworks like Bootstrap or Foundation, and this package reduces that. Furthermore, if we ever need to change the markup we don't need to hunt down every instance in our code to do so (DRY).

This functionality can be achieved by using @include, but that can be annoying when your templates are broken down into a ton of tiny templates scattered about.

Installation

Begin by installing this package through Composer.

{
    "require": {
        "crhayes/blade-partials": "0.*"
    }
}

Next open up app/config/app.php, comment out the Illuminate View Service Provider, and add the one from this package:

'providers' => array(
    //'Illuminate\View\ViewServiceProvider',
    ...
    'Crhayes\BladePartials\ViewServiceProvider',
)

And that's it!

Creating a Partial

Partials start with the @partial('path.to.view') directive, which accepts the view you want the partial to extend from, and end with the @endpartial directive.

@partial('partials.panel')
    @block('title', 'This is the panel title')

    @block('body')
        This is the panel body.
    @endblock
@endpartial

Blocks within partials behave the same way as sections within templates. They capture a piece of data that will be rendered into the extended view.

Rendering a Partial

We use the @render('block-to-render') directive to render a block of content that was provided via the respective @block directive. Note that we can also provide a default value.

<div class="panel">
    <div class="panel-heading">
        <h3 class="panel-title">@render('title', 'Default Title')</h3>
    </div>
    <div class="panel-body">
        @render('body', 'Default Body')
    </div>
</div> 

Full Example

This example will include our partial HTML file. Notice that we can create as many instances of the partial as we like.

// index.blade.php
@extends('layouts.master')
 
@section('content')
    @partial('partials.panel')
        @block('title', 'This is the panel title')
 
        @block('body')
            This is the panel body.
        @endblock
    @endpartial
    
    @partial('partials.panel')
        @block('title', 'This is a second panel title')
 
        @block('body')
            And we will have different content in this body.
        @endblock
    @endpartial
@stop
 
// /partials/panel.blade.php
<div class="panel">
    <div class="panel-heading">
        <h3 class="panel-title">@render('title')</h3>
    </div>
    <div class="panel-body">
        @render('body')
    </div>
</div> 

Nesting Partials

You can also do some cool things by nesting partials. For example:

// index.blade.php
@extends('layouts.master')

@section('content')
    @partial('partials.danger-panel')
        @block('title', 'This is the panel title')
    
        @block('body')
            This is the panel body.
        @endblock
    @endpartial
@stop

// partials/danger-panel.blade.php
@partial('partials.panel')
    @block('type', 'danger')

    @block('title')
    	Danger! @render('title')
    @endblock
@endpartial

// partials/panel.blade.php
<div class="panel panel-@render('type', 'default')">
    <div class="panel-heading">
        <h3 class="panel-title">@render('title')</h3>
    </div>
    <div class="panel-body">
        @render('body')
    </div>
</div> 

统计信息

  • 总下载量: 7.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 52
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 52
  • Watchers: 4
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-02-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固