Symfony includes static html in template

I've just started learning how to use Symfony, which I think is straight forward, but I have a small problem with the templating engine. I want to include a static HTML snippet in one of my branch templates in Symfony ( 2.5.6

). So far I've created a static subfolder inside the resource directory (this may change, but definitely won't be inside the view folder). However, I cannot do it, I always get the error unable to find template

. I find the documentation on this is a bit sparse (see http://symfony.com/doc/current/book/templating.html#including-other-templates ) and also the docs branch can't help me with that. I'm not even sure if I can use the @Bundle magic notation or if I should be in the view folder, since no notation is allowed in the include tag ../

.

I tried the following (and several options):

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

      

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

I know http://github.com/kgilden/KGStaticBundle which will probably solve my problem, but I can't believe this isn't possible with the default setting.

EDIT: I just tried to include a regular template file from the same directory with just the name of the template file (as done in the docs, see the link above). However, I get an error and complain about what it expects bundle:section:template.format.engine

as its format. Is there a mistake in the docs?

+3


source to share


3 answers


It looks like the poster has found a solution while typing. I guess I'll leave it here for a bit.

Here's how to create and use tween namespaces: http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html

By default, each package gets its own namespace pointing to the view directory. You can add additional directories by configuring app / config.yml

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

      



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

I won't go into 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 within the bundle itself does not require config.yml configuration:

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'));        
}
}

      

+4


source


Try the following:

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

      



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

).

+1


source


I found a solution!

In the branch configuration, you can set paths and give them a name.

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" %}

      

+1


source







All Articles