Integrate Twig with ZF2., (*1)
, (*2)
Install
Using composer
Add following in your composer.json:, (*3)
{
"require": {
"appt/twig": "1.*"
}
}
And enable module in your application.config.php:, (*4)
return array(
'modules' => array(
'ApptTwig',
)
);
Usage
Configuration
Templates
ApptTwig support adding templates using common for Zend Framework 2 TemplateMapResolver and TemplatePathStack loaders., (*5)
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',
),
),
),
);
Twig options
You can change twig engine option in config:, (*6)
return array(
'appt' => array(
'twig' => array(
'engine_options' => array(
'debug' => true, // turn on debug mode
)
),
),
);
Twig extensions
Also it is possible to add new twig extensions:, (*7)
return array(
'appt' => array(
'twig' => array(
'extension_manager' => array(
'factories' => array (
'ZendViewHelpers' => 'ApptTwig\Service\Extension\ZendViewHelpersFactory'
)
),
)
)
);
All extensions must implement Twig_ExtensionInterface., (*8)
Provided services
ApptTwig provide following services:, (*9)
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., (*10)
Rendering
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., (*11)