preflow/twig 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

preflow/twig

最新稳定版本:v0.13.1

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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固