havit/craft-twig-components
Composer 安装命令:
composer require havit/craft-twig-components
包简介
Twig components extension
README 文档
README
This is a PHP package for automatically create Twig components as tags. This is highly inspired from Laravel Blade Components.
Installation
You can install the package via Composer:
composer require havit/twig-components
Configuration
This package work only with Silex and PHP 7.1
$app['twig.options'] = [ 'debug' => true, 'cache' => __DIR__.'/../storage/cache/twig', 'components' => [ 'path' => 'components', 'namespace' => 'App\View', ], ]; $app->register(new \App\Provider\TwigComponentsServiceProvider());
To enable the package just pass your Twig environment object to the function and specify your components folder relative to your Twig templates folder.
SILEX
Create Provider
use Pimple\{Container, ServiceProviderInterface}; class TwigComponentsServiceProvider implements ServiceProviderInterface { public function register(Container $app) { $app->extend('twig', function (\Twig_Environment $twig, $app) { $twig->addExtension(new \Havit\TwigComponents\Extension\ComponentExtension($twig)); $twig->setLexer(new \Havit\TwigComponents\Lexer\ComponentLexer($twig)); return $twig; }); } }
Usage
The components are just Twig templates in a folder of your choice (e.g. components) and can be used anywhere in your Twig templates. The slot variable is any
content you will add between the opening and the close tag.
Named slots
{# /components/card.twig #} <div {{ attributes.class('bg-white shadow p-3 rounded') }}> <h2 {{ title.attributes.class('font-bold') }}> {{ title }} </h2> <div> {{ body }} </div> </div> {# /index.twig #} <x-card> <x-slot name="title" class="text-2xl">title</x-slot> <x-slot name="body">Body text</x-slot> </x-card>
Also with the standard syntax.
{# /index.twig #} {% x:card %} {% slot:title with {class: "text-2xl"} %} Title {% endslot %} {% slot:body %} Title {% endslot %} {% endx %}
Attributes
You can pass any attribute to the component in different ways. To interprate the content as Twig you need to prepend the attribute name with a : but it works
also in other ways.
snake-case variables will be converted to kebabCase
Call
<x-button :class="'btn-' ~ 'success'" type="{{'this' ~ 'works' ~ 'too'}}" action="or this" this="{{'this' ~ 'does'}}{{ 'not work' }}" data-key="foo" > Submit </x-button>
Component
<a href="{{ action }}" {{ attributes.class('btn btn-sm me-3') }} data-type="{{ type }}" data-key="{{ dataKey }}"> {{ $slot }} </a>
Compiled
<a href="or this" class="btn btn-sm me-3 btn-success" data-type="thisworkstoo" data-key="foo"> Submit </a>
Component Class
<?php namespace App\View; use App\Application; class Test extends \Havit\TwigComponents\View\Component { public $title = ''; public function __construct(string $title = 'xyz') { $this->title = $title; } public function handle(Application $app) { // Do something $this->title = $app->trans($this->title); } public function template(): string { return 'components/test.twig'; } } ## Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate. ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
havit/craft-twig-components 适用场景与选型建议
havit/craft-twig-components 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugins」 「craftcms」 「twig-components」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 havit/craft-twig-components 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 havit/craft-twig-components 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 havit/craft-twig-components 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Expand, collapse, change the status of, or delete multiple blocks in a Matrix field simultaneously.
GraphQL authentication for your headless Craft CMS applications.
Supercharged text field validation.
Integrate with Snipcart.
Atomic UX Twig components with registry-aligned markup and role CSS
Form domain Twig components: labels, inputs, compounds, and native interaction CSS
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-28