定制 jadu/twig-style 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

jadu/twig-style

Composer 安装命令:

composer require jadu/twig-style

包简介

Jadu Twig coding style configuration for Twig-CS-Fixer

README 文档

README

Jadu Twig style is powered by Twig-CS-Fixer.

Installation

  1. Require the jadu/twig-style dev dependency:
composer require --dev jadu/twig-style
  1. Add the twig-cs-fixer config file .twig-cs-fixer.php:
<?php

use Jadu\Style\Twig\Standard\JaduStandard;
use TwigCsFixer\Config\Config;
use TwigCsFixer\File\Finder;
use TwigCsFixer\Ruleset\Ruleset;

$finder = Finder::create()
    ->in(__DIR__ . '/src')
    ->ignoreVCSIgnored(true);

$config = new Config();
$config->setFinder($finder);

$ruleset = new Ruleset();
$ruleset->addStandard(new JaduStandard());
$config->setRuleset($ruleset);

return $config;
  1. Add .twig-cs-fixer.cache to your project's .gitignore file.

Usage

Dry run

To lint your project's twig files, run the following dry run command:

vendor/bin/twig-cs-fixer lint

This command will return a list of twig-cs-fixer violations and is recommended for build tasks.

Fix

To fix any reported fixable violations, run the following fix command:

vendor/bin/twig-cs-fixer lint --fix

Jadu Twig Coding Standard Rules

This standard is based on the official Twig coding standards, with the following additions and changes:

Block spacing and new lines

There must be one new line before block tags and one new line after endblock tags.

{% block aside %}
    <div class="aside">

        {% block aside_inner %}

            {% block before_primary_supplements %}
            {% endblock %}

            {% block primary_supplements %}
            {% endblock %}

            {% block after_primary_supplements %}
            {% endblock %}

        {% endblock %}

    </div>
{% endblock %}

The following exceptions apply:

  • Inline blocks are allowed.

    <body class="{% block body_classes %}{{ bodyClasses }}{% endblock %}">
  • Comments on the line above block tags are allowed.

    {# This block adds a container around the aside #}
    {% block aside_container %}

Endblock names

Any endblock tags must be followed by the name of the block they are closing.

{% block aside_container %}
{% endblock aside_container %}

No spaceless tags

The spaceless tag was deprecated in Twig 1.38 and 2.7.31 and an equivalent spaceless filter was introduced. Usages of the spaceless tag must be replaced with the equivalent apply spaceless filter.

{% apply spaceless %}
{% endapply %}

No filter tags

The filter tag was deprecated in Twig 1.402 and 2.93 in favour of the apply tag, which behaves identically to filter except that the wrapped template data is not scoped. Usages of the filter tag must be replaced with the equivalent apply tag.

{% apply lower|escape('html') %}
    <strong>UPPERCASE TEXT</strong>
{% endapply %}

{# outputs "&lt;strong&gt;uppercase text&lt;/strong&gt;" #}

Punctuation spacing

A single space is required after the opening and before the closing of a hash.

{{ { 'foo': 'bar', 'baz': 'qux' } }}

The following exceptions apply:

  • Empty hashes must not contain any whitespace.
{% set emptyHash = {} %}

Variable names

Variable naming conventions are not enforced.

Development

The rules in the Jadu\Style\Twig\Rule\Development namespace are provided for development purposes to help with maintaining Jadu Twig style.

You will need to update your project's twig-cs-fixer config file .twig-cs-fixer.php to enable these rules, as non-fixable rules are disabled by default.

<?php

use Jadu\Style\Twig\Rule\Development\TokenTypeRule;
use TwigCsFixer\Config\Config;
use TwigCsFixer\File\Finder;
use TwigCsFixer\Ruleset\Ruleset;

$finder = Finder::create()
    ->in(__DIR__ . '/src')
    ->ignoreVCSIgnored(true);

$config = new Config();
$config->setFinder($finder);

$ruleset = new Ruleset();
$ruleset->addRule(new TokenTypeRule());
$config->setRuleset($ruleset);

$config->allowNonFixableRules();

return $config;
  • TokenTypeRule helps you see how a twig template is tokenized by Twig-CS-Fixer by mapping token types to values.

Footnotes

  1. https://symfony.com/blog/better-white-space-control-in-twig-templates#added-a-spaceless-filter

  2. https://twig.symfony.com/doc/1.x/tags/filter.html

  3. https://twig.symfony.com/doc/2.x/tags/filter.html

jadu/twig-style 适用场景与选型建议

jadu/twig-style 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.87k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 02 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 jadu/twig-style 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2024-02-20