定制 esheep/twigs 二次开发

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

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

esheep/twigs

Composer 安装命令:

composer require esheep/twigs

包简介

A framework-agnostic Twig extension that bundles useful functions, filters, and tags into a single entry point.

README 文档

README

A framework-agnostic Twig extension that bundles useful functions, filters, and tags into a single entry point.

Installation

composer require esheep/twigs

Setup

Register the extension with your Twig environment:

$twig->addExtension(new \eSheep\Twigs\Extension());

Constructor Options

use eSheep\Twigs\Extension;
use TalesFromADev\TailwindMerge\TailwindMerge;

$twig->addExtension(new Extension(
    // Optional: inject a pre-configured TailwindMerge instance
    tailwindMerge: new TailwindMerge(additionalConfiguration: [...]),

    // Optional: default options for the merge_attributes filter
    mergeOptions: [
        'remove_duplicate_classes' => true, // default: true
        'tailwind_merge' => true,           // default: true
    ],
));

Functions

html_tag / tag

Generates an HTML tag.

{{ html_tag('div', {class: 'container', id: 'main'}) }}
{# <div id="main" class="container"></div> #}

{{ tag('p', {text: 'Hello <world>'}) }}
{# <p>Hello &lt;world&gt;</p> #}

{{ tag('p', {html: '<strong>Bold</strong>'}) }}
{# <p><strong>Bold</strong></p> #}
  • text: HTML-encodes the value and uses it as the tag body.
  • html: Uses the value as raw HTML tag body.
  • All other keys are rendered as HTML attributes.

html_attr / attr

Renders an attribute array as an HTML attribute string.

<div{{ attr({class: ['a', 'b'], id: 'main', 'data-value': '42'}) }}>
{# <div class="a b" id="main" data-value="42"> #}

html_cva / cva

Creates a CVA (Class Variant Authority) instance for managing component class variants.

{% set button = cva('btn', {
    color: {primary: 'btn-primary', danger: 'btn-danger'},
    size: {sm: 'btn-sm', lg: 'btn-lg'}
}, [], {color: 'primary', size: 'sm'}) %}

<button class="{{ button.apply({size: 'lg'}) }}">
{# <button class="btn btn-primary btn-lg"> #}

Filters

html_attr / attr

Modifies the attributes of an existing HTML tag string.

{{ '<div class="old">'|attr({class: 'new', id: 'test'}) }}
{# <div id="test" class="old new"> #}

merge_attributes

Merges two attribute arrays with optional class deduplication and Tailwind class conflict resolution. The result can be passed to attr() to render as HTML attributes.

{% set base = {class: 'p-4 text-red-500', id: 'hero'} %}
{% set extra = {class: 'p-2 font-bold', 'hx-get': '/api'} %}

{# Basic merge — deduplicates classes and resolves Tailwind conflicts by default #}
<div{{ base|merge_attributes(extra)|attr }}>
{# <div id="hero" class="text-red-500 p-2 font-bold" hx-get="/api"> #}

{# Override options per call #}
{% set merged = base|merge_attributes(extra, {tailwind_merge: false}) %}
{% set merged = base|merge_attributes(extra, {remove_duplicate_classes: false}) %}

Options (third argument, overrides extension defaults):

Key Type Default Description
remove_duplicate_classes bool true Deduplicate class names after merging
tailwind_merge bool true Resolve Tailwind CSS class conflicts

tailwind_merge / tw_merge

Merges Tailwind CSS classes, intelligently resolving conflicts.

{{ 'px-2 py-1 bg-red-500'|tw_merge('p-4') }}
{# bg-red-500 p-4 #}

{{ 'text-red-500'|tw_merge('text-blue-500') }}
{# text-blue-500 #}

Tags

{% html_tag %} / {% tag %}

Block-level tag generation. Captures content between the tags.

{% tag 'nav' with {class: 'main-nav', role: 'navigation'} %}
    <a href="/">Home</a>
    <a href="/about">About</a>
{% endtag %}

{% html_tag 'section' with {id: 'hero', class: 'full-width'} %}
    <h1>Welcome</h1>
{% endhtml_tag %}

Attribute Normalization

Attributes are normalized automatically:

  • class strings are split into arrays and deduplicated on merge.
  • style strings are parsed into property/value pairs.
  • Data attributes like data-foo are collapsed into data: {foo: ...}.
  • removeClass pseudo-attribute removes classes:
{{ '<div class="a b c">'|attr({removeClass: 'b'}) }}
{# <div class="a c"> #}

Supported data attribute prefixes: aria, data, data-hx, data-ng, hx, ng.

Provenance

Some code in this package is ported from Craft CMS. See PROVENANCE.md for details on what was ported and from which commit.

License

MIT. See LICENSE.

esheep/twigs 适用场景与选型建议

esheep/twigs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 esheep/twigs 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-10