[RFC] laminas-form Twig integration


Integrate laminas-form with Twig template.


Extend Twig with the laminas-form functions and helpers.
I’m not sure if some parts of laminas-view are required to make the integration works.


I did a small demo but I couldn’t make it work, all it renders is form labels.
It was inspired from @akrabat demo (outdated)

Hi everybody, I made it work, I hope it is the right way to do it, I updated the repository:

// ... get twig environment

$helper = new FormElement();
$renderer = new PhpRenderer();

$helpers = $renderer->getHelperPluginManager();
$config  = new HelperConfig();
$serviceManager = $config->configureServiceManager($helpers);


$twigEnvironment->registerUndefinedFunctionCallback(function($name) use($serviceManager){
	$helper = $serviceManager->get($name);
	$callable = [$helper, '__invoke'];
	$options  = ['is_safe' => ['html']];
	$fn = new TwigFunction($name, $callable, $options);
	return $fn;


An example of this integration as a twig extensions could be like the following:

class LaminasFromTwigExtension extends AbstractExtension
	private $formElement;
	private $renderer;
	private $config;

	 * @inherit
	public function __construct(FormElement $formElement, PhpRenderer $renderer, HelperConfig $config)
		$this->formElement = $formElement;
		$this->renderer = $renderer;
		$this->config = $config;

	public function getFunctions()
		$fns = [];
		$pluginManager = $this->pluginManager();
		$serviceManager = $this->serviceManager($pluginManager);
		$aliases = $this->configure($serviceManager);
		$options  = ['is_safe' => ['html']];
		foreach ($aliases as $name => $definition) {
			if ($serviceManager->has($name)) {
				$plugin = $serviceManager->get($name);
				$callable = [$plugin, '__invoke'];
				$fns[] = new TwigFunction($name, $callable, $options);
		return $fns;

	private function pluginManager(): HelperPluginManager
		return $this->renderer->getHelperPluginManager();

	private function serviceManager(HelperPluginManager $pluginManager): ServiceManager
		return $this->config->configureServiceManager($pluginManager);

	private function configure(ServiceManager $serviceManager): array
		$config = $this->config->toArray();
		return $config['aliases'];