nia/templating-twig

使用twig模板实现的模板化。

此包的规范存储库似乎已不存在,因此该包已被冻结。

1.0.1 2016-11-06 17:21 UTC

This package is not auto-updated.

Last update: 2022-03-05 05:16:40 UTC


README

nia框架中使用Twig模板引擎的组件。

安装

使用Composer安装此包。

	composer require nia/templating-twig

测试

要运行单元测试,请使用以下命令

$ cd /path/to/nia/component/
$ phpunit --bootstrap=vendor/autoload.php tests/

如何使用

以下示例展示了如何创建一个简单的服务提供者(基于nia/dependencyinjection组件),该提供者注册了一个模板工厂。

	/**
	 * Sample provider for templating.
	 */
	class TemplateProvider implements ProviderInterface
	{

	    /**
	     *
	     * {@inheritDoc}
	     *
	     * @see \Nia\DependencyInjection\Provider\ProviderInterface::register()
	     */
	    public function register(ContainerInterface $container)
	    {
	        $factory = new SharedFactory(new ClosureFactory(function (ContainerInterface $container) {
	            $loader = new Twig_Loader_Filesystem([
	                '/path/to/templates',
	                '/path/to/other/templates'
	            ]);

	            $environment = new Twig_Environment($loader, [
	                'cache' => '/path/to/compilation-cache'
	            ]);

	            return new TwigTemplateFactory($environment);
	        }));

	        $container->registerService(TemplateFactoryInterface::class, $factory);
	    }
	}


	// somewhere in a presenter
	// [...]
	$template = $container->get(TemplateFactoryInterface::class)->create('index.html');

	$content = $template->fetch([
	    'key1' => 'value1',
	    'key2' => 'value2'
	]);

	$response->setContent($content);