Jake N
Jake N

Reputation: 10583

Template Partials in Symfony 2

Are there such things as partials in Symfony 2, reusable templates from anywhere, effectively?

I have found include http://twig.sensiolabs.org/doc/tags/include.html but this only allows the rendering of a template in a specific directory structure.

What I really want a folder that contains all my partial templates, rather than lumping them into my main views?

So I want to be able to do something like

{% include "Bundle:Default:Partials:view.html.twig" %}

Update

I do not want to use the enforced structure of Bundle:Controller:Template structure. I do not want to use this as it means putting all my template partials in with my main view templates. I need something that lets me do Bundle:Controller:PartialDir:Template

Upvotes: 4

Views: 16687

Answers (4)

watlf
watlf

Reputation: 11

To include the controller, you'll need to refer to it using the standard string syntax for controllers (i.e. bundle:controller:action):

{{ render(controller(
    'App\\Controller\\ArticleController::recentArticles',
    { 'max': 3 }
)) }}

Upvotes: 0

Strayobject
Strayobject

Reputation: 662

I know this is old, but the way to achieve what OP asks is the following:
Rather than doing
bundle:controler:partialDir:template
we have to switch it slightly to achieve:
{% include 'Bundle:PartialDir/Controller:Template' %}

Upvotes: 0

rogden
rogden

Reputation: 21

In Symfony 2.4 (the version I am currently using but it probably works in other 2.x versions as well) you can do the following:

{% include '::_partials/partial.html.twig' %}

or in Symfony 2.2+ using the include function

{{ include('::_partials/partial.html.twig') }}

This will look for the partial.html.twig template inside of the app/Resources/views/_partials directory. You can obviously name the _partials directory whatever you want. It also works without the '::' prefix.

Upvotes: 2

dteoh
dteoh

Reputation: 5922

You can already do that. The symfony2 docs has a section describing how to do this.

http://symfony.com/doc/current/book/templating.html#including-other-templates

Upvotes: 10

Related Questions