承接 conedevelopment/blade-filters 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

conedevelopment/blade-filters

Composer 安装命令:

composer create-project conedevelopment/blade-filters

包简介

Use filters easily in your blade templates.

README 文档

README

Use string filters easily in Laravel Blade.

If you have any question how the package works, we suggest to read this post: Laravel Blade Filters.

Getting started

You can install the package with composer, running the composer require conedevelopment/blade-filters command.

Using the filters

You can use the filters in any of your blade templates.

Regular usage:

{{ 'john' | ucfirst }} // John

Chained usage:

{{ 'john' | ucfirst | substr:0,1 }} // J

{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31

Passing non-static values:

{{ $name | ucfirst | substr:0,1 }}

{{ $user['name'] | ucfirst | substr:0,1 }}

{{ $currentUser->name | ucfirst | substr:0,1 }}

{{ getName() | ucfirst | substr:0,1 }}

Passing variables as filter parameters:

$currency = 'HUF'

{{ '12.75' | currency:$currency }} // HUF 12.75

Built-in Laravel functionality:

{{ 'This is a title' | slug }} // this-is-a-title

{{ 'This is a title' | title }} // This Is A Title

{{ 'foo_bar' | studly }} // FooBar

Limitations

Echos

Laravel supports three types of echos. Raw – {!! !!}, regular – {{ }} and escaped (legacy) – {{{ }}}. Filters can be used only with regular echos. Also, filters cannot be used in blade directives directly.

Why? Raw should be as it is. Forced escaping should be escaped only, without modification.

Bitwise operators

Bitwise operators are allowed, but they must be wrapped in parentheses, since they are using the same "pipe operator".

{{ ('a''b') | upper }} // C

The Filters

About the filters

Filters are string functions, that are defined in the Pine\BladeFilters\BladeFilters facade. It has several reasons, that are discussed in the Create custom filters section.

The available filters

The package comes with a few built-in filters, also the default Laravel string methods can be used.

Currency

{{ '17.99' | currency:'CHF' }} // CHF 17.99

{{ '17.99' | currency:'€',false }} // 17.99 €

Passing false as the second parameter will align the symbol to the right.

Date

{{ '1999/12/31' | date }} // 1999-12-31

{{ '1999/12/31' | date:'F j, Y' }} // December 31, 1999

Lcfirst

{{ 'Árpamaláta' | lcfirst }} // árpamaláta

Unlike PHP's default lcfirst(), this filter works with multi-byte strings as well.

Reverse

{{ 'ABCDEF' | reverse }} //FEDCBA

Substr

{{ 'My name is' | substr:0,2 }} // My

{{ 'My name is' | substr:3 }} // name is

Trim

{{ '   trim me    ' | trim }} // trim me

Ucfirst

{{ 'árpamaláta' | ucfirst }} // Árpamaláta

Unlike PHP's default ucfirst(), this filter works with multi-byte strings as well.

Supported built-in Str functions

Create custom filters

As it was mentioned before, every filter is a method that can be called through the Pine\BladeFilters\BladeFilters facade. It has several reasons why is this approach better, but let's take the most important ones:

  • It's easy to define custom filters by extending the facade with the BladeFilters::macro(),
  • No extra files, autoloading or class mapping, it's enough to use any service provider to define filters,
  • By default Laravel provides a bunch of handy methods that we can use as filters.

Parameter ordering

PHP is not very strict regarding to function's parameter ordering and this way it's easier to coordiante or override them. Also, sometimes it happens with Laravel's string functions. It's important that only those functions can be used, that accept the parameters in the following order:

  1. The value to be transformed
  2. Any other parameter if needed

For example:

BladeFilters::macro('filterName', function ($value, $param1 = 'default', $param2 = null) {
    return ...;
});

{{ 'string' | filterName:1,2 }}

Defining custom filters

Since the filters are only methods that are defined in the Str facade and the BladeFilters class, to create filters, you need to create a macro in a service provider's boot() method.

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        BladeFilters::macro('substr', function ($value, $start, $length = null) {
            return mb_substr($value, $start, $length);
        });
    }
}

Contribute

If you found a bug or you have an idea connecting the package, feel free to open an issue.

conedevelopment/blade-filters 适用场景与选型建议

conedevelopment/blade-filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 156.34k 次下载、GitHub Stars 达 493, 最近一次更新时间为 2020 年 09 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 156.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 493
  • 点击次数: 14
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 493
  • Watchers: 7
  • Forks: 28
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-16