swayok/laravel-blade-directives 问题修复 & 功能扩展

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

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

swayok/laravel-blade-directives

Composer 安装命令:

composer require swayok/laravel-blade-directives

包简介

Additional Blade directives for Laravel

README 文档

README

This package provides additional directived for Laravel Blade template engingine. Among directives are fully-functional partials, translations and dot.js constructions.

Installation

Add require to composer.json and run composer update

"require": {
    "swayok/laravel-blade-directives": "~1.0",
}

Configuration

Service provider for Laravel 5.6+

Automatically added via package auto-discovery.

Service provider for Laravel < 5.6

Add \LaravelBladeDirectives\LaravelBladeDirectivesServiceProvider::class to your config/app.php into providers array

Shortcuts

@breakpoint 
// add breakpoint to view (allows you to debug compiled views)

@string($id, $parameters = [], $locale = null)
@trans($id, $parameters = [], $locale = null)
// {{ trans($id, $parameters, $locale) }}

@html($id, $parameters = [], $locale = null)
@unescaped($id, $parameters = [], $locale = null)
// {!! trans($id, $parameters, $locale) !!}

@key($array, $key, $default, $escaped = true)
// {{ array_get($array, $key, $default) }} - when $escaped === true
// {!! array_get($array, $key, $default) !!} - when $escaped === false

Examples:

@string('frontend.footer', ['year' => 2019], 'en')
@trans('frontend.footer', ['year' => 2019])
@html('frontend.lang.' . app()->locale())
@unescaped('frontend.app_name')

@key($settings, 'language', 'en') 
// {{ array_get($settings, 'language', 'en') }}
@key($settings, 'language', 'en', false)
// {!! array_get($settings, 'language', 'en') !!}

Partials

The idea behind partials is possibility to use sub-template several times in a single blade template without using a @view() directive that require a view file to be created. Sometimes you do not need separate view file for simple template like table row. Partials also useful to make big template a bit cleaner.

Partials declared and used like sections with exception that partials receive only data you explicitely pass into @yieldPartial and some shared data provided by $__env->getShared() (\Illuminate\View\Factory->getShared()). Under the hood partial is a closure that renders some blade template using passed data. It does not create files and all code is placed inside current blade template.

Declare partial

@partial('name')
    <div>{{ $var1 }}</div>
@endPartial

Yield partial

@yieldPartial('name', ['var1' => 'this is partial'])
@yieldpartial('name', ['var1' => 'this is another partial'])

Result

<div>this is partial</div>
<div>this is another partial</div>

Note: all directives have both cameCase and lowercase versions

dot.js directives

It is a pain using dot.js inserts inside blade templates. You need to add @ before each dot.js insert, also you cannot insert php code inside such inserts. Template becomes a real mess sometimes.

The idea with this directives is to make blade templates that contain dot.js inserts more clean and visually better. In addition there must be a possibility to insert some php code inside directive.

Simple inserts

@jsEcho(it.value)
// {{= it.value }}
@jsecho(it.value || 'default')
// {{= it.value || 'default' }}

@jsEchoEncoded(it.value)
// {{! it.value }}
@jsechoencoded(it.value || "default")
// {{! it.value || "default" }}

@jsEval(it._some_name = 'value')
// {{ it._some_name = 'value' }}
@jseval(it._some_name = "value")
// {{ it._some_name = "value" }}

@jsIf(it.value)
// {{? it.value }}
@jsif(it.value === 'example')
// {{? it.value === 'example' }}

@jsIfSimple(it.value)
// {{? it.value }}
@jsifsimple(it.value === 'example')
// {{? it.value === 'example' }}

@jsElseIf(it.value)
// {{?? it.value }}
@jselseif(it.value === 'example')
// {{?? it.value === 'example' }}

@jsElseIfSimple(it.value)
// {{?? it.value }}
@jselseifsimple(it.value === 'example')
// {{?? it.value === 'example' }}

@jsElse
@jselse
// {{??}}

@jsEndIf
@jsendif
// {{?}}

@jsForEach(it.array as key => value)
// {{~ it.array :value:key }}
@jsforeach(it.array as value)
// {{~ it.array :value }}

@jsEndForEach
@jsendforeach
// {{~}}

@jsPartial(partial_name_without_spaces)
@jspartial(partial_name_without_spaces)
// {{##def.partial_name_without_spaces:

@jsEndPartial
@jsendpartial
// #}}

@jsEchoPartial(partial_name_without_spaces)
@jsechopartial(partial_name_without_spaces)
// {{#def.partial_name_without_spaces}}

Complex inserts with PHP code

PHP code inside dot.js directive must begin with `. and end with .` (the quote is the one that placed on tilda ~ button on keyboard and is called backtick).

Using backticks allows using normal quotes and double quotes without problems caused by incorrect quotes escaping. Also if you're using PHP Storm you can configure Code Style for backticks that differs from other quotes and shows dot.js directives in unique way (configs are in the next section).

Examples:

$phpVar = 'var_string'
SOME_CONSTANT = 'constant_string'
$phpNumber = 5

@jsIf(it.value === "` . $phpVar . `")
// {{? it.value === "var_string" }}
// Resulting php code in compiled template is: <?php echo '@{{? it.value === "' . ($phpVar) . '" }}'; ?>

@jsIf(it.value === '` . $phpVar . `')
// {{? it.value === 'var_string' }}
// Resulting php code in compiled template is: <?php echo '@{{? it.value === \'' . ($phpVar) . '\' }}'; ?>

@jsif(it.value === "` . SOME_CONSTANT . `")
// {{? it.value === "constant_string" }}

@jsIf(it.value >= ` . ($phpNumber - 1) . ` && it.value <= ` . ($phpNumber + 1) . `)
// {{? it.value >= 4 && it.value <= 6}}

Directives that support PHP code:

  • jsEcho/jsecho
  • jsEchoEncoded/jsechoencoded
  • jsEval/jseval
  • jsIf/jsif
  • jsElseIf/jselseif

Directives that does not support PHP code:

  • jsIfSimple/jsifsimple
  • jsElseIfSimple/jselseifsimple
  • jsForEach/jsforeach
  • jsPartial/jspartial
  • jsEchoPartial/jsechopartial
  • Other directives that does not allow parameters (like jsEndIf, jsEndForEach)

PHP Storm Configuration (Blade directives)

Open Settings > Languages & Frameworks > PHP > Blade. Untick Use default settings. Open Directives tab.

Each directive have next inputs:

  • Name
  • Has parameter
  • Prefix
  • Suffix

Note: directives with same configs are grouped in Names - you will need to create a record for each directive name.

Names: string, trans, html, unescaped
Has parameter: true
Prefix: <?php echo trans(
Suffix: ); ?>

Names: key
Has parameter: true
Prefix: <?php $fn = function(array $array, $key, $default = null, bool $escaped = true) {} ; $fn(
Suffix: ); ?>

Names: partial
Has parameter: true
Prefix: <?php $str =
Suffix: ); ?>

Names: yieldPartial, yieldpartial
Has parameter: true
Prefix: <?php $fn = function(string $partialName, array $partialData = []) {}; $fn(
Suffix: ); ?>

Names: breakpoint, endPartial, endpartial
Has parameter: false

Names: jsEcho, jsEchoEncoded, jsEval, jsForEach, jsIf, jsElseIf, jsIfSimple, jsElseIfSimple, jsPartial, jsEchoPartial 
Has parameter: true
Prefix: <?php echo `
Suffix: `; ?>

Names: jsecho, jsechoencoded, jseval, jsforeach, jsif, jselseif, jsifsimple, jselseifsimple, jspartial, jsechopartial
Has parameter: true
Prefix: <?php echo `
Suffix: `; ?>

Names: jsElse, jsEndIf, jsEndForEach, jselse, jsendif, jsendforeach, jsEndPartial, jsendpartial
Has parameter: false

PHP Storm Configuration (colors for directives)

Open Settings > Editor > Color Scheme > Blade. Interesting parameters here are:

  • Directive
  • Text block delimiter

I added background to both so that directives are clearly visible in Blade template.

Open Settings > Editor > Color Scheme > PHP.

Interesting parameter here is Shell command. This is rarely used feature in PHP but it will be useful for showing dot.js directives differently from usual Blade directives. Just add some background color. This will display parameters within dot.js directive with specific background color. I personally like this possibility because it makes normal and dot.js directives visually different.

swayok/laravel-blade-directives 适用场景与选型建议

swayok/laravel-blade-directives 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 896 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「laravel」 「partials」 「blade」 「dotJs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 swayok/laravel-blade-directives 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 swayok/laravel-blade-directives 我们能提供哪些服务?
定制开发 / 二次开发

基于 swayok/laravel-blade-directives 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 896
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-15