felicity-php/felicity-twig

对 Twig 进行轻量级封装。基本功能是实例化 twig 并提供访问接口以渲染模板和扩展 Twig。

1.0.0 2017-11-26 01:19 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:42:07 UTC


README

Felicity Twig 是对 Twig 的轻量级封装。基本上它只是实例化 twig 并提供访问接口以渲染模板和扩展 Twig。

用法

使用静态 get 方法获取 Twig 环境,或者如果通过依赖注入传递了 Twig::getInstance(),则使用 getTwig 方法。

<?php

use felicity\twig\Twig;

$twig = Twig::get();

$twig->getLoader()->addPath('path/to/custom/template/dir', 'mynamespace');

$html = $twig->render('@mynamespace/path/to/template.twig');

配置

Felicity Twig 依赖于 Felicity Config,因此您可以控制 Twig 的行为方式。

<?php

use felicity\config\Config;

// Enabled Twig debug
Config::set('felicity.twig.debug', true);

// Set a string or array of template paths for the loader
Config::set('felicity.twig.templatePaths', []);

// Set the charset. UTF-8 is the default
Config::set('felicity.twig.charset', 'UTF-8');

// Set a different base template class than the default
Config::set('felicity.twig.base_template_class', 'Custom_Twig_Template');

// Set a cache location for Twig to use.
// It is HIGHLY recommended that you set this
Config::set('felicity.twig.cache', 'path/to/cache/dir');

// Whether to reload the template if the original source changed.
// You should probably leave this as default
Config::set('felicity.twig.auto_reload', true);

// Enable strict variables
Config::set('felicity.twig.strict_variables', true);

// Change autoescape preferences
Config::get('felicity.twig.autoescape', 'html');

// Disable optimizations (probably don't do this)
Config::get('felicity.twig.optimizations', 0)

许可证

版权所有 2017 BuzzingPixel, LLC

遵循 Apache License 2.0(“许可证”);除非适用法律要求或书面同意,否则不得使用此文件,除非符合许可证。您可以在 https://apache.ac.cn/licenses/LICENSE-2.0 获取许可证副本。

除非适用法律要求或书面同意,否则在许可证下分发的软件按“原样”提供,不提供任何明示或暗示的保证或条件。有关许可协议中规定的权限和限制的具体语言,请参阅许可证。