zf-commons/zfc-twig

此包最新版本(1.2.2)没有提供许可证信息。

Zend Framework 2 模块,提供 Twig 渲染策略和扩展,可以从模板中渲染操作或触发事件。

1.2.2 2014-04-29 17:40 UTC

README

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

安装

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

配置

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

文档

设置 Twig 扩展

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

// in module configuration or autoload override
return array(
    'zfctwig' => array(
        'extensions' => array(
            // 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_Chain 以便将加载器链接在一起。使用 文件系统加载器 设置路径为 module/Application/view 的便利默认设置应该适用于大多数实例。如果您希望将其他加载器添加到链中,您可以通过将服务管理器别名添加到 loaders 配置键来注册它们。

// in module configuration or autoload override
return array(
    'zfctwig' => array(
        'loaders' => array(
            'MyTwigFilesystemLoader'
        )
    )
);

// in some module
public function getServiceConfiguration()
{
    return array(
        'factories' => array(
            'MyTwigFilesystemLoader' => function($sm) {
                return new \Twig_Loader_Filesystem('my/custom/twig/path');
            }
        )
    );
}

使用 ZF2 视图辅助函数

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

{# 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 文件夹中找到。

注意事项

ZF2 并不支持使用视图辅助函数的多重渲染器。作为解决方案,ZfcTwig 注册了自己的 HelperPluginManager,该管理器扩展了默认的 Zend\View\HelperPluginManager 并将其作为对等管理器添加。这允许 ZfcTwig 为需要渲染器的视图辅助函数注册自己的渲染器,并回退到默认管理器以用于不需要渲染器的视图辅助函数。

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