interface TemplateEnvironment
{
function render($template, $context = [], $request = null);
+ function addFunction($name, $callable);
}
class TemplateEnvironmentTwig implements TemplateEnvironment
/** @var IPF_Router */
private $router;
+ private $customFunctions = [];
+
public function __construct(IPF_Settings $settings, array $processors, array $apps, LoaderInterface $loader, IPF_Router $router)
{
$this->settings = $settings;
return $template->render($context);
}
+ public function addFunction($name, $callable)
+ {
+ $this->customFunctions[$name] = $callable;
+ }
+
private function createEnvironment()
{
$options = array(
$twig->addFunction(new TwigFunction('trans', function ($str) {
return __($str);
}));
+ foreach ($this->customFunctions as $name => $callable) {
+ $twig->addFunction(new TwigFunction(
+ $name,
+ function (/* $context, ...$args */) use ($callable) {
+ return call_user_func_array($callable, func_get_args());
+ },
+ ['needs_context' => true]
+ ));
+ }
}
}