saeven/zfc-twig

Laminas 模块,提供 Twig 渲染策略和扩展,以便从模板中渲染操作或触发事件

dev-master / 4.x-dev 2024-03-20 03:22 UTC

This package is auto-updated.

Last update: 2024-09-20 04:34:02 UTC


README

ZfcTwig 是一个模块,它将 Twig 模板引擎与 Laminas / Zend Framework 集成。

信息

这是一个 ZF-Commons/ZfcTwig 的分支。我添加了 ZF3 支持,因此该模块可以与 Laminas / Zend Framework 2 和 3 一起工作。如果您发现了一个错误,请报告它,只需在 gitter 上给我发消息或打开一个 PullRequest。

安装

  1. "kokspflanze/zfc-twig": "dev-master" 添加到您的 composer.json 文件中,然后运行 php composer.phar update
  2. ZfcTwig 添加到您的 config/application.config.php 文件中的 modules 键下。

配置

ZfcTwig 默认提供合理的配置,但通过 zfctwig 配置键提供可选配置。有关所有可用选项的详细信息,请参阅 模块配置文件 类。

文档

设置 Twig 扩展

可以通过向 extensions 配置键添加 FQCN 来在 Twig 中注册扩展,这正是 ZfcTwig 扩展注册的方式。

// in module configuration or autoload override
return [
    'zfctwig' => [
        'extensions' => [
            // an extension that uses no key
            'My\Custom\Extension',

            // an extension with a key so that you can remove it from another module
            'my_custom_extension' => 'My\Custom\Extension'
        ]
    ]
];

配置 Twig 加载器

默认情况下,ZfcTwig 使用 Twig\Loader\ChainLoader,以便可以将加载器链接在一起。使用设置在 module/Application/view文件系统加载器 的方便默认设置应该适用于大多数实例。如果您希望向链中添加其他加载器,可以通过向 loaders 配置键添加服务管理器别名来注册它们。

// in module configuration or autoload override
return [
    'zfctwig' => [
        'loader_chain' => [
            'MyTwigFilesystemLoader'
        ]
    ]
];

// in some module
public function getServiceConfiguration()
{
    return [
        'factories' => [
            'MyTwigFilesystemLoader' => function($sm) {
                return new \Twig\Loader\FilesystemLoader('my/custom/twig/path');
            }
        ]
    ];
}

使用 ZF 视图助手

通过 ZfcTwig\Twig\FallbackFunction 函数支持使用 ZF 视图助手。

{# Simple view helper echo #}
{{ doctype() }}

{# Echo with additional methods #}
{{ headTitle('My Company').setSeparator('-') }}

{# Using a view helper without an echo #}
{% do headTitle().setSeparator('-') %}

{# Combining view helpers #}
{% set url = ( url('my/custom/route') ) %}

示例

骨架应用程序的 .twig 文件示例可以在 examples 文件夹中找到。

注意事项

ZF 不太支持使用视图助手的多重渲染器。作为解决方案,ZfcTwig 注册了自己的 HelperPluginManager,该管理器扩展了默认的 Laminas\View\HelperPluginManager 并将默认管理器添加为对等管理器。这允许 ZfcTwig 在需要它的视图助手中注册自己的渲染器,并在不需要渲染器的视图助手中回退到默认管理器。

作为注意事项,您 必须 将需要渲染器的视图助手注册到 ZfcTwig 中。一个示例可以在 config/module.config.php 中看到,其中 HelperConfig 为默认导航助手注册了 ZfcTwig。