kevinquillen/twig-partials
Composer 安装命令:
composer require kevinquillen/twig-partials
包简介
Django 6 style partial template support for Twig.
README 文档
README
A Twig extension that adds Django-style partial template rendering. Define reusable template fragments and render them inline or via AJAX requests.
Requirements
- PHP 8.3 or higher
- Twig 3.15 or higher
Installation
composer require kevinquillen/twig-partials
Usage
Defining Partials
Use the partialdef tag to define a reusable fragment within a template:
{% partialdef view_count %}
<span class="views">{{ video.views }} views</span>
{% endpartialdef %}
Rendering Partials Inline
Use the partial tag to render a defined partial within the same template:
{% partialdef info %}
<div class="video-info">
<h2>{{ video.title }}</h2>
<span>{{ video.views }} views</span>
</div>
{% endpartialdef %}
<main>
{% partial info %}
</main>
Full Example
{# video.twig #} {% partialdef view_count %} <span id="view-count">{{ video.views }} views</span> {% endpartialdef %} {% partialdef comments_section %} <div id="comments"> {% for comment in video.comments %} <p>{{ comment.text }}</p> {% endfor %} </div> {% endpartialdef %} <article> <h1>{{ video.title }}</h1> {% partial view_count %} <video src="{{ video.url }}"></video> {% partial comments_section %} </article>
Rendering the Full Template
$twig->render('video.twig', ['video' => $video]);
This renders the entire template including all partials.
Rendering Only a Fragment (AJAX)
The extension supports rendering only a specific partial using fragment syntax:
$twig->render('video.twig#view_count', ['video' => $video]);
This renders only the view_count partial, which is useful for AJAX updates.
Setup
Standard Twig
use Twig\Environment; use TwigPartials\Extension\PartialExtension; use TwigPartials\Loader\PartialLoader; $loader = new PartialLoader(['/path/to/templates']); $twig = new Environment($loader); $twig->addExtension(new PartialExtension());
Symfony
Register the extension as a service:
services: TwigPartials\Extension\PartialExtension: tags: ['twig.extension']
To enable fragment rendering, replace the default loader:
services: TwigPartials\Loader\PartialLoader: decorates: twig.loader.native_filesystem arguments: ['@TwigPartials\Loader\PartialLoader.inner']
How It Works
The partialdef tag compiles into a separate method on the template class. When you use partial, it calls that method and outputs the result. The content has access to the same context variables as the rest of the template.
When using the fragment syntax (template.twig#fragment_name), the loader recognizes the fragment identifier and the runtime can render only that specific partial instead of the full template.
Running Tests
composer install ./vendor/bin/phpunit
License
MIT
kevinquillen/twig-partials 适用场景与选型建议
kevinquillen/twig-partials 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kevinquillen/twig-partials 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kevinquillen/twig-partials 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-10