I have a lot of templates in a bundle and they all extend the same layout.html.twig. Rather than have to define:
{% extends 'MyBundle::layout.html.twig' %}
at the top of every template, is there a way to do configure this?
I suspect that I would need to create a pre- or postExecute()-like method based on an event listener that extended the template before rendering.
This is not a good idea, because not every template has to extend a layout: there are some “system” templates like form_div_layout.html.twig that are not meant to extend anything. Besides, not every template you write has to extend a layout; you'll encounter a lot of use cases for small embeddable templates to reference from other templates.
If you will try to force a layout on each template, you'll have to write some logic to exclude “system” and embeddable templates so that they don't extend anything, and you'll have to do the same for your layout template too, so that it doesn't extend itself infinitely. In this case you'll reverse the problem: instead of defining explicitly which layout to extend in each template, you'll have to explicitly state which templates should not extend your layout. This will get very messy very fast.
To grasp this idea more completely, you need to know that, behind the scenes, templates are really just PHP classes. Does it make sense to you to make a particular class the parent for every other classes, and then explicitly state which classes should not extend this parent?
But if I haven't convinced you not to go this way, there is a Twig setting which lets you set the base template class for all templates:
twig:
base_template_class: Your\Layout\ClassName\Here
You can extend \Twig_Template or implement \Twig_TemplateInterface and have some “fun” for several hours, after which I hope you'll be convinced to abandon this idea whatsoever. Good luck. :)
Related
I'm a bit confused as I've seen a few varying methods posted online.
I have a bundle created with easyextends in src/Application/Sonata/SonataMediaBundle, which extends the SonataMediaBundle in the vendors.
The default template displays
This is the gallery index template. Feel free to override it.
This file can be found in SonataMediaBundle:Gallery:index.html.twig.
so I've added src/Application/Sonata/SonataMediaBundle/Resources/views/Gallery/index.html.twig in my bundle
and this works and overrides, so why all these various other ways like How to override Sonata Media Bundle templates?
You are using the correct way. To quote from symfonys How to Override Templates from Third-Party Bundles
To override the bundle template, just copy index.html.twig template from the bundle to app/Resources/AcmeBlogBundle/views/Blog/index.html.twig (the app/Resources/AcmeBlogBundle directory won't exist, so you'll need to create it). You're now free to customize the template.
And for more detailed/ complex overriding behaviour have a look at How to Use Bundle Inheritance to Override Parts of a Bundle
I wouldn't worry to much about other solutions unless you are not able to get the results you require using this method but I cant think of one.
(a) A bundle (bundle 1) holds the main parent Twig template.
(b) Another bundle (bundle 2) holds a load of controllers and those controllers each render a Twig template that's inside that bundle (bundle 2). That template also extends from the main parent one mentioned above (bundle 1). Hard coding an absolute path to the main parent template is fine.
(c) The parent template (inside bundle 1) also embeds/includes another template, which is stored in the other bundle (bundle 2).
Can I set a relative path for part (c) above so that if I created another bundle (bundle 3) it would automatically work (parent template includes templates from bundle that calls it)? It would pick up the main parent as it is an absolute path, but would it pick up part (c) above? Obviously I can't use an absolute path for the templates that the parent one is embedding/including.
If I understand properly, you are asking how to extend templates easily while having dependencies from one bundle to another.
I'd advise you to read this documentation about overriding bundle templates.
Once read this, you will notice a link that points to the [bundle inheritance documentation].
As an example, we have 3 bundles with explicit names:
- GenericSiteBundle
- GenericUserBundle
- SpecificSiteBundle
We want to create a generic structure for an app that could be used by other apps and we also want them to be different therefore we need to be able to change the layout (header, footer, columns, etc.).
Template structure would be something like GenericUserBundle:Security:login.html.twig extends GenericSiteBundle:Layout:simple.html.twig.
You wish to change the simple layout with a specific one; all you need to do is declare GenericSiteBundle as the parent of SpecificSiteBundle and create SpecificSiteBundle:Layout:simple.html.twig. Make sure only the bundle name is different otherwise it won't find the template in the child bundle.
Also, you will love this parent() twig function. It allows you to grab the content of the parent block and add it into the child block.
It's not exactly what you want to do but this proper way to handle inheritance.
I'm (still) a beginner with Symfony and have been reading about three-level inheritance with Twig.
If you define blocks in your bundle their names might be bundle specific and not match block names defined in your app base.html.twig or other bundles, or even the names could be the same but the usage could be different.
Has anyone found this to be a problem?
Are there ways to manage this, will overriding templates under app/ help?
Are there any conventions for block names or anything else that will minimise maintenance problems?
Block names should have some sort of relation to whats going to place within the block. so if you have css styles within a block you would call it Stylesheets, and for javascript files call it JavaScript. As far bundles, create a layout.html.twig within the views folder and extend the base.html.twig.
I want to make an application based on a main bundle leaving the possibility for other developers to make their own bundles to implement other features.
Symfony 2.0 seems a good choice for that however I cannot figure out how to let the bundles to work together while preserving the decoupling.
In the MainBundle I will create a Controller which generates a Users-List like the one below:
user1 edit remove
user2 edit remove
....
How to let third-parties bundles to add their custom buttons to this list?
For example an AvatarBundle may want to add a button to upload of the Image, a SendEmailBundle my want to add a button to send an email to the user and so on.
How to preserve the bundle independency? How can I do that?
Thanks a lot,
Massimo
As far as I know, there are only two ways of changing/adding functionality in provided bundles.
Change the code
Overriding the template/controller
In this case, the second seems far preferable.
The ways to override a template are:
Define a new template in app/Resources
Create a child bundle and override the template
If you also want the override controllers, the second way is the only way to go.
It's also my personal preference, since it's cleaner then putting specific stuff in the general app-folder, in my opinion.
Anyway, it is far better explained in the documentation of the FOSUserBundle:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.md
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md
And ofcourse, this cookbook article:
http://symfony.com/doc/current/cookbook/bundles/inheritance.html
Hope this helps,
Dieter
If you know which parts of your bundle will be extended, you can use services (in the same way the tiwg bundle is using them for adding new templating filters/tags/helpers).
Your bundle will then 'scan' the services defined in the DIC (which have a defined tag) and then call them to get informations (button definitions in your case).
I am starting a new project using Symfony2. I am adding some basic twig templates like my CSS template (with assetic to trigger sass -- nifty) and my header/footer template. My question is, should this sort of thing go into app/Resources or into Acme/MainBundle/Resources?
I am sure I can do either if I really wanted to, but I'd like to know what the correct way to do it is.
app
Resources
<A. here?>
src
Acme
MainBundle
Resources
<or B. here?>
It's really a matter of preference. I tend to keep global views in app/Resources. Does your MainBundle contain anything else besides views? If not, I think that's more reason to use app/Resources and avoid unnecessary bundle pollution.