5

I just started to learn how to use Symfony which i believe is straight forward but i have a little problem with the templating engine. I want to include a static HTML fragment in one of my twig templates in Symfony (2.5.6). For now i created a static subfolder inside the resources directory (this might change but it definitely won't be inside the view folder). However i can't get this done, i always end up with an unable to find template error. I find the documentation on this subject a bit sparse (see http://symfony.com/doc/current/book/templating.html#including-other-templates) also the twig docs can't help me out on this one. I'm not even sure if i can use the magic @Bundle notation or if i have to reside in the view folder as ../ notation is not allowed within the include tag.

I tried the following (and a couple of variations):

{{ include('@FashionMediaBundle/Resources/static/pricing.html') }}

I guess symfony cannot handle raw html in include but i could use a php template without any template tags as well, so the question is just how to specify a location outside the view folder.

I know of http://github.com/kgilden/KGStaticBundle which will probably solve my issue but i can't believe that is not achievable with the default configuration.

EDIT: i just tried to include a regular template file from the very same directory specifying just the name of the template file (as done in the docs, see link above). Still i get an error also complaining it expects bundle:section:template.format.engine as its format. Is there an error in the docs?

4
  • 1
    If your bundle is called FashionMediaBundle then remove Bundle part, twig namespaced path is called without it, like this: @FashionMedia Commented Nov 14, 2014 at 12:54
  • @xurshid29 no it's not but you have to omit the @. i have a working testcase for another template to include which works like FashionMediaBundle:Static:include.html.php but not for FashionMedia:Static:include.html.php Commented Nov 14, 2014 at 14:13
  • no it's not but read this :) Commented Nov 14, 2014 at 15:13
  • Ah i see. Also works for resources within the views directory. Commented Nov 14, 2014 at 15:42

3 Answers 3

4

Looks like the poster found the solution while I was typing. I guess I'll leave this here for just a bit.

Creating and using twig namespaces is discussed here: http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html

By default, each bundles get's it's own namespace pointing to the views directory. You can add additional directories by adjusting app/config.yml

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    paths:
        "%kernel.root_dir%/../../cerad2/src/Cerad/Bundle/FashionMediaBundle/Resources/static": FashionMedia

Load the template with: '@FashionMedia/pricing.html'

I won't go into all the details but you can also use a compiler pass(http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html) to add additional paths from inside the bundle itself without having to adjust config.yml:

class Pass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
    $bundleDirAction = $container->getParameter('cerad_api01__bundle_dir') . '/Action';

    $twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');

    $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($bundleDirAction, 'CeradApi01'));        
}
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

{{ include('FashionMediaBundle:Resources:static:pricing.html') }}

If you want to put this file in another directory I suggest you to use a symbolic link (I don't know if Twig can include a file which is not in the Resources directory).

6 Comments

Still the same result.
Is your file located at the FashionMediaBundle/Resources/views/static/pricing.html path?
No, as mentioned above it is NOT in the views directory this is the actual complication. I have a testcase {{ include('FashionMediaBundle:Static:include.html.php') }} which works a file inside the views directory, but 1) is rendered as twig not php and 2) in the doc they include without the bundle and section but i cannot do that...
I'm sorry, I don't know if you can use a file not in the views``directory. I still suggest you to use a symbolic link if you want to put the file in another directory than views`.
Thanks once again i found a solution, see my answer
|
2

I found the Solution!

In the twig configuration you can set paths and assign a name to them.

twig:
    [... twig options here ...]
    paths:
        "%kernel.root_dir%/../[path to static]": static_pages

And then you can include them in

 {% include "@static_pages/pricing.html.twig" %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.