The directory structure for bundles specifies that views should be stored in <your-bundle>/Resources/views. But then the best practices for template locations says that, actually, I should store them in app/Resources/views. I can see the conveniences of doing the latter, but I don't understand:
If I am organizing things into bundles, aren't I reducing the bundles' portability by "de-bundling" the views?
The examples show that index.html.twig is easier to write than AcmeDemoBundle::index.html.twig, but what if I also have FooBundle::index.html.twig? I still need a way to specify which index.html.twig I want, right?
The documentation is correct. Store reusable bundle's templates in <your-bundle>/Resources/views and your project's templates in app/Resources/views. There is no conflict.
If you decide to create a bundle make sure it can be reused (that's the main purpose of the bundle). Otherwise keep using your AppBundle. AppBundle will not have two /index.html.twig's.
Related
While we are using Assetic with Twig templating, we want to delay the actual processing until the last moment for various reasons. So instead of using the javascripts, css tags of assetic we created something like this
{{ add_asset (['public/js/prototype-handler.js', 'public/js/shipping-method.js'], 'js') }}
Anyhow, the idea is that at the end of the response event we will process and inject all assets to the content. However, right now I'm stuck at how to add these assets to assetic so they can be processed and returned with the result file(s).
I have checked some other bundles and what they are doing right now is to render the assets via twig like this:
AssetManagementBundle
However, it doesn't seem to be an optimal approach to this. I wonder if there is a better way or not?
For people who may encounter this same need, you want to use createAsset of the AssetFactory.
Then you can loop through the created assets and do what you want with them.
For more information and working code, please check our bundle here
I'm not sure to completely understand your needs but you could be interested by the AsseticInjectorBundle, it allows you to tag your resources files in a configuration file and add it by adding the tag where you want in your assetic markup, in your layout.
I don't think dealing with resources in php is a great idea and it's better to do this directly in your layout coupled with some configuration file.
I am new to symfony2 so please forgive me if this is a stupid question.
Symfony2 is organised in bundles - so everything is a bindle right?
Based on this I have created the following bundles in order to have a simple login mechanism:
App
The main bundle will contain all global functionality
User
Will be used to represent users
So the bundles work correctly and all is fine.
Now I cant figure out the best way to add a layout/theme structure to the site.
I obviously need some global assets such as header, nav and foooter. But additionally, there needs to be some global css style sheets, jquery etc.
The most obvious place bundle is App - but how do i make all other bundles inherit the theme from this bundle. For example, the user bundle template needs to extend the App bundle etc.
The idea for bundles is that they are modular and self contained, therefore how can this be achieved
I personally use like this:
There is MainBundle (in your case, it is App) which does global services, twig extensions and layout. Global assets are included in this file.
Main layout of all the other bundles extends the layout of MainBundle. Templates inside each bundle extend to the main layout of it which extend the layout of MainBundle. For example,
- MainBundle
- views
- layout.html.twig
- UserBundle
- views
- layout.html.twig (extends to MainBundle/layout)
- show.html.twig (extends to UserBundle/layout)
- friends.html.twig (extends to UserBundle/layout)
Everything is explained in the official docs: http://symfony.com/doc/current/templating.html
The basic global view (templates) resources are placed in the following dir:
app/Resources/views/index.html.twig
If you have a specific purpose, or bundles, templates, place them in the subdirs, e.g.:
app/Resources/views/blog/index.html.twig
And if you want to keep everything in the bundle (must for reusable code), use this convention:
[VendorName/]YourBundle/Resources/views/Blog/index.html.twig
(Of course, any name, except the ".html.twig" extension for Twig, may be changed to your liking)
The strategy I prefer is to organize my application in a single bundle. If you have no intention of re-using distinct standalone features across multiple applications then this is the most appropriate way. Having a "UserBundle" in your own application namespace probably doesn't make sense. You're adding a lot of extra structure that's serving you no advantage. Consider this instead:
- MainBundle
- Controller
- UserController
- OtherController
- Resources
- views
- layout.html.twig
- User
- show.html.twig
- update.html.twig
- friends.html.twig
- Other
- some_other_view.html.twig
In this case the templates under the controller directories would extend MainBundle::layout.html.twig.
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.