Integrate Twig with ZF2.
Add following in your composer.json:
{
"require": {
"appt/twig": "1.*"
}
}
And enable module in your application.config.php:
return array(
'modules' => array(
'ApptTwig',
)
);
ApptTwig support adding templates using common for Zend Framework 2 TemplateMapResolver and TemplatePathStack loaders.
return array(
'appt' => array(
'twig' => array(
'default_template_suffix' => 'twg', // you can change file extension used by TemplatePathStack
'template_path_stack' => array(
'dir/1/',
'dir/2/'
),
'template_map' => array(
'layout/layout' => 'dir/layout.twig',
'error' => __DIR__ . 'dir/error.twig',
),
),
),
);
You can change twig engine option in config:
return array(
'appt' => array(
'twig' => array(
'engine_options' => array(
'debug' => true, // turn on debug mode
)
),
),
);
Also it is possible to add new twig extensions:
return array(
'appt' => array(
'twig' => array(
'extension_manager' => array(
'factories' => array (
'ZendViewHelpers' => 'ApptTwig\Service\Extension\ZendViewHelpersFactory'
)
),
)
)
);
All extensions must implement Twig_ExtensionInterface.
ApptTwig provide following services:
appt.twig.renderer -- renderer service compatible with PhpRenderer;
appt.twig.renderer_strategy -- renderer strategy;
appt.twig.resolver -- resolver service extend Zend\View\Resolver\AggregateResolver and used for resolving templates;
appt.twig.extension_manager -- used for managing twig extensions, it extend Zend\ServiceManager\AbstractPluginManager.
When you setup resolvers in your config ApptTwig is ready for rendering view templates as it was regular PhpRenderer. All you need is create templates in your view directory.