I used Twig without Symfony for some time now. Now I want to switch my whole application to symfony and use my old templates and i have problems with relative paths which I usually use with Twig:
I use something like this with standalone Twig:
function show_template() {
global $lang;
# $lang is "es" or "nl" etc
$templates_dir = 'templates/'.$lang;
# fallback dir if template is not found in the main location
$fallback_dir = 'templates/en'
$loader = new Twig_Loader_Filesystem(array($templates_dir, $fallback_dir));
$twig = new Twig_Environment($loader, array(
# 'cache' => './cache',
));
# $file is relative path to $templates_dir
return $twig->render($file, $params);
}
show_template('index.html.twig');
and I also use relative paths in templates. i.e. io index.html.twig
{% include 'includes/header.html.twig' %}
hello world
{% include 'includes/footer.html.twig' %}
this is pretty simple but using this in Symfony2 is not possible in this form as I can see.
As I can see I have to use something like this:
$this->render('AcmeHelloBundle:Default:index.html.twig', $params);
And templates have to be changed to use following:
{% include '@AcmeHello/Default/includes/header.html.twig' %}
hello world
{% include '@AcmeHello/Default/includes/footer.html.twig' %}
I don't mind adapting my code/templats adding some new templating logic templates but I need to have flexible paths. Questions:
- How can I use in PHP code relative paths to templates to be able to use changing templates_dir depending on language/whatever
- How can I use relative path in templates?
- How can I have a fallback templates/fallback directories?