定制 preflow/twig 二次开发

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

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

preflow/twig

Composer 安装命令:

composer require preflow/twig

包简介

Preflow Twig adapter — Twig template engine implementation

README 文档

README

Twig 3 adapter for Preflow. Implements TemplateEngineInterface and ships co-located CSS/JS support via {% apply css %} / {% apply js %} filters and {{ head() }} / {{ assets() }} functions.

Installation

composer require preflow/twig

Requires PHP 8.4+ and Twig 3.

What's included

Component Description
TwigEngine TemplateEngineInterface implementation wrapping Twig\Environment
PreflowExtension {% apply css %}, {% apply js %}, {{ head() }}, {{ assets() }}
ComponentExtension {{ component('Name', {props}) }} — renders Preflow components
HdExtension {{ hd.post(...) }}, {{ hd.get(...) }} — hypermedia action helpers
TranslationExtension {{ t('key') }}, {{ tc('key', 'Component') }} — i18n helpers

TwigEngine

use Preflow\Twig\TwigEngine;
use Preflow\View\AssetCollector;
use Preflow\View\NonceGenerator;

$assets = new AssetCollector(new NonceGenerator(), isProd: true);

$engine = new TwigEngine(
    templateDirs: [__DIR__ . '/templates', __DIR__ . '/app/pages'],
    assetCollector: $assets,
    debug: false,
    cachePath: __DIR__ . '/storage/twig-cache',  // null = no cache
);

$html = $engine->render('blog/post.twig', ['post' => $post]);
$engine->exists('partials/nav.twig');    // bool
$engine->getTemplateExtension();         // 'twig'

Co-located styles and scripts

Use {% apply css %} and {% apply js %} anywhere in a template. The content is registered with the AssetCollector and nothing is output at that point.

{# templates/blog/post.twig #}

{% apply css %}
.post-title { font-size: 2rem; font-weight: 700; }
.post-body  { line-height: 1.7; }
{% endapply %}

{% apply js %}
document.querySelector('.post-body a[href^="http"]')
  ?.setAttribute('target', '_blank');
{% endapply %}

{% apply js('head') %}
window.analyticsId = {{ post.id }};
{% endapply %}

<h1 class="post-title">{{ post.title }}</h1>
<div class="post-body">{{ post.body|raw }}</div>

JS position argument: 'body' (default), 'head', or 'inline'.

Layout with head() and assets()

{{ head() }} renders JS registered for the <head>. {{ assets() }} renders all collected CSS plus body JS — place it just before </body>.

{# templates/_layout.twig #}
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>{% block title %}App{% endblock %}</title>
  {{ head() }}
</head>
<body>
  {% block content %}{% endblock %}
  {{ assets() }}
</body>
</html>

asset_url()

The asset_url() template function generates a URL to a public asset. Register it via your AppServiceProvider to point at the correct public path.

<link rel="stylesheet" href="{{ asset_url('css/app.css') }}">
<img src="{{ asset_url('images/logo.png') }}" alt="Logo">

Register in your service provider:

$engine->addFunction(new TemplateFunctionDefinition(
    name: 'asset_url',
    callable: fn (string $path) => '/public/' . ltrim($path, '/'),
    isSafe: false,
));

Engine configuration

Set APP_ENGINE=twig in your .env (this is the default). Preflow's Application will automatically create a TwigEngine and register all extension providers.

preflow/twig 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-11