filhocodes/twig-stack-extension
Composer 安装命令:
composer require filhocodes/twig-stack-extension
包简介
Implement content stacks into Twig templates.
README 文档
README
This extension allows you to define a section of a "base template" that will
receive contents from all the other pages, either via include, embed or
extends.
The reason behind this package is that phive/twig-extensions-deferred doesn't
really have the features I wanted. For those who doesn't know, this extension
allows you to say that a block in your template will be evaluated at the end of
the template, and not while it is parsing. It also serves as a learning project,
since it is my first Twig Extension.
This project is also HEAVILY related to the package aygon/twig-stack. It's
kinda like a refactor of their code into Twig 2.*, with my opinionated choices.
CHANGELOG v2.0.0
Version 2.0.0 enables support for both PHP 8.0 and PHP 8.1, while removing support for PHP 7.3 and bellow. If you are still using those old PHP versions, you will need to install the version 1.0.0 of this package.
Installation
You can install this package via composer:
composer require filhocodes/twig-stack-extension
If you are using Symfony, you should be able to configure it by adding a entry
into services.yaml:
services: FilhoCodes\TwigStackExtension\TwigStackExtension: class: 'FilhoCodes\TwigStackExtension\TwigStackExtension' tags: ['twig.extension']
In either way, you can always add the extension directly into your Twig environment:
use FilhoCodes\TwigStackExtension\TwigStackExtension; /** @var $twig \Twig\Environment */ $twig->addExtension(new TwigStackExtension());
Usage
In your base template, define a stack using {% stack %}
<html lang="en"> <head> <title>My template</title> {% stack styles %} </head> <body> {% stack scripts %} </body> </body> </html>
Then, in files that either extend your template, or are included by any other,
you may use {% push %} and {% prepend %} to add contents to the end and the
beginning of the stack, respectively.
{% extends 'base.twig.html' %}
{% push scripts %}
<script></script>
{% endpush %}
{# include 'partial.twig.html' #} {% push styles %} <link /> {% endpush %}
To prevent duplicated code, like in files that are included or embed multiple
times you can add an "id" to the block declaration in order to the extension to
identify the block as unique. Just keep in mind that we will not verify the
contents of said block, neither if it is a push or prepend. So watch out for
possible collision-naming bugs.
{# embed 'component.twig.html' #} {# The following contents will be rendered only once #} {% push scripts component_definition_script %} <script> MyPlugin = { /* ... */ } </script> {% endpush %} {# The following contents will be rendered as many times as this file is embed, probably each with some different initialization logic... #} {% push scripts %} <script> MyPlugin.init(); </script> {% endpush %}
filhocodes/twig-stack-extension 适用场景与选型建议
filhocodes/twig-stack-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.76k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2020 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 filhocodes/twig-stack-extension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 filhocodes/twig-stack-extension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-24