wol-soft/twig-include-dir
Composer 安装命令:
composer require wol-soft/twig-include-dir
包简介
Include all twig templates within a directory
README 文档
README
twig-include-dir
Include all twig templates within a directory
Features
- Provide a directory with multiple .twig templates and all templates will be included
- Add recursive keyword to include all templates within a directory recursive
- known variable handling as known from include using the keywords only and with
Requirements
- Requires Twig >= 2.7
- Requires PHP >= 7.2
Installation
The recommended way to install twig-include-dir is through Composer:
$ composer require wol-soft/twig-include-dir
Why?
An example use case could be: you set up a site using bootstrap with many modals. Now you don't need to throw all your modals together in a file or include each modal manually but instead you can separate your modals by using one file for each modal. Throw all modals in a modal-directory and simply include the whole directory. Adding a new modal? No problem, just create a new template file in your modal-directory.
Getting started
To use twig-include-dir you need to add the include-dir token parser to your Twig Environment first:
<?php /* ... */ $loader = new FilesystemLoader(__DIR__ . DIRECTORY_SEPARATOR); $twig = new Environment($loader); $twig->addTokenParser(new \TwigIncludeDir\IncludeDirTokenParser()); /* ... */
Afterwards you can start using the added token includeDir in your templates:
<div class="modal-container"> {% includeDir '/modals' %} </div>
The files in the directory will be included alphabetically.
Recursive usage
To include all files within a given directory recursive simply add the keyword recursive to your include statement:
<div class="modal-container"> {% includeDir '/modals' recursive %} </div>
Now also the modals from the directories /modals/user and /modals/system etc. will be included.
Caution: The templates will be included alphabetically as well, including the directories. Thus the template /modals/footer.twig will be included before the templates from the directory /modals/system followed by /modals/user followed by a possible /modals/zebraHeader.twig. It is recommended to use twig-include-dir only for templates which do not require a specific order.
Variables
As known from the Twig Core include you can control the available variables with the keywords with and only (compare: include)
<div class="modal-container"> {# only the foo variable will be accessible #} {% includeDir '/modals' recursive with {'foo': 'bar'} only %} </div>
wol-soft/twig-include-dir 适用场景与选型建议
wol-soft/twig-include-dir 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 731 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wol-soft/twig-include-dir 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wol-soft/twig-include-dir 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 731
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-31