nia/templating-twig

Templating implementation using twig.

This package's canonical repository appears to be gone and the package has been frozen as a result.

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

Component to use the Twig template engine in the nia framework.

Installation

Require this package with Composer.

	composer require nia/templating-twig

Tests

To run the unit test use the following command:

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

How to use

The following sample shows you how to create a simple service provider (based on the nia/dependencyinjection component) which registers a template factory.

	/**
	 * 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);