My twig extension code is as follows
/**
* @return array
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('bsPanelTitle', array($this, 'bsPanelTitle')),
];
}
/**
* @param $headline
* @return string
*/
public function bsPanelTitle($headline)
{
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../Resources/views/Common/Placeholder');
$twig = new \Twig_Environment($loader);
return $twig->render('xtitle.html.twig', ['headline' => $headline]);
}
My question is:
Is there a better way to reach AppBundle/Resources/views/Common/Placeholder folder from a Twig extension function?
Since this question is tagged with symfony2, I'm assuming you are indeed talking about symfony2 project, and if that is the case you can registering your own namespaces with twig, using the twig config, something like this:
twig:
# ...
paths:
"%kernel.root_dir%/../pathFromProjectRootToPlaceholder/Placeholder": placeholder
This creates an alias to the folder pathFromProjectRootToPlaceholder/Placeholder and then you can use it to render your templates like this:
//from controller
return $this->render(
'@placeholder/index.html.twig',
$data
);
OR
{# from twig template #}
{% include '@placeholder/index.html.twig' %}
You can add multiple paths to the same alias, but for more information check the official cookbook on symfony 2 site: http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html
Hope this helps,
Alexandru Cosoi
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments