In the Template Naming and Locations section in the Symfony docs it says:
Symfony2 uses a bundle:controller:template string syntax for templates. This allows for several different types of templates, each which lives in a specific location:
- AcmeBlogBundle:Blog:index.html.twig: This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a colon (:), mean the following:
- AcmeBlogBundle: (bundle) the template lives inside the AcmeBlogBundle (e.g. src/Acme/BlogBundle); Blog: (controller) indicates that the template lives inside the Blog subdirectory of Resources/views;
- index.html.twig: (template) the actual name of the file is index.html.twig.
I want to parse a twig template and persist the html into the property of a doctrine entity during my data fixtures bootstrapping process like so:
// let's say it finds ./Data/Product/camera_description.html.twig
$productDescriptionTemplate = __DIR__.sprintf(
'/Data/Product/%s_description.html.twig',
$product->getName()
);
$product->setDescription(
$this->container->get('templating')->render(
$productDescriptionTemplate,
array()
)
);
$em->flush();
Which throws the following exception:
# would actually be an absolute path
[InvalidArgumentException]
Template name "./Data/Product/camera_description.html.twig
" is not valid (format is "bundle:section:template.format.engine").
Yes I could move the product description templates to path/to/bundle/Resources/views/
but I am more interested in whether it is possible to circumvent this convention: Is there a way to give the twig templating engine the relative or absolute path to a twig template and have it render it not having to use the convention bundle:controller:template
?
You can also create a new namespace as described here : http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html
for example :
paths:
"%kernel.root_dir%/../Data/Product/": product
which should allow you to write :
'product::%s_description.html.twig',
or
'@product/%s_description.html.twig'
If you don't want use bundle:controller:template
syntax, then you could try using twig directly:
$product->setDescription(
$this->container->get('twig')->render(
$productDescriptionTemplate,
array()
)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With