Correct way to override default template in Sonata - symfony

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.

Related

Override template of a Symfony BlockBundle SimpleBlock

I recently configured my website project to use Symfony CMF with Sonata Admin.
I'm now populating my website with random editable simple blocks, but can't figure out the way to override their rendering template.
There are two stacks of templates, once for use with the frontend editing CreateBundle and once without. The one for CreateBundle have a _createphp.html.twig at the end. Are you sure you overwrite the correct template? See https://github.com/symfony-cmf/BlockBundle/tree/master/Resources/views/Block
When I debug such things, i try to edit the template in the vendor folder (make a syntax error, write "HELLO" or whatever) just to see if i am even trying to overwrite the correct template.
In the title of this question you mix sonata and cmf. The cmf block bundle builds on sonata block bundle, but provides a couple of block types. Are you sure you have the paths right?

Payum Bundle : How to change the view of capture action in symfony2

I am using PAYUM Bundle for the payment gateway integration, and its basic example is working fine, But now I want integrate the payum bundle in application by changing the payum capture action layout and adding extra field payment detail entity.
PAYUM BUNDLE with AUTHORIZE.NET GATEWAY.
Please can anyone help me out.
Thanks all in advance.
The payum templates is not kept in the bundle but in the payum lib itself. Standard templates inheritance does not work here.
There is no simple way to do so in version 0.9 (which is shipped with Sylius right now). You have to overwrite the whole CaptureAction class. In 0.10 it is possible to change templates by overwriting container parameters like for layout, or stripe js page.
In the sandbox you can find an example of layout modification
You have to override views of PayumBundle.
How to do that?
You have to reproduce the folder structure of the bundle you want to override inside your /src/Path/To/Your/Bundle/Resources/views and place inside it the twig file you want to override.
Example
Let's say your bundle name is FooBundle and you want to override PayumBundle payment.twig.html (I'm just making an example, don't know if there's a file named that way). Let's say, also, that this twig is inside /vendor/Path/To/PayumBundle/Resources/views/Payment/payment.twig.html.
What you have to do is create inside /src/Path/To/Your/Bundle/Resources/views/Payment/payment.twig.html
Symfony2 will look, at first, into your bundle for overrided views: if any will took yours and ignore bundles one. Otherwise It will took bundle's one.

define custom filesystem path for twig templates

I've already read How does Symfony2 detect where to load templates from? and another ton of articles on the web
The question was quite similar to mine, but the answer wasn't comprehensive.
I need to override a bundle-defined template inside another custom bundle. In other words I want specify another path where symfony have to look before looking in app/Resources when loading templates.
Workflow should be something like this:
first: /src/My/Bundle/Resources/views/example.html.twig (or /src/My/Bundle/OriginalBundle/views/example.html.twig)
then: /app/Resources/OriginalBundle/views/example.html.twig
finally: /src/Original/Bundle/Resources/views/example.html.twig
The standard app/Resources -> src/Original/Bundle isn't enough.
sorry for my poor english and thank you
There's a native feature to do exactly what you want in a nice way. Escentially you can add a twig namespace to the twig configuration in app/config.yml like this:
twig:
# ...
paths:
"%kernel.root_dir%/../vendor/acme/foo-bar/templates": foo_bar
This creates an alias to the folder vendor/acme/foo-bar/templates and then you can use it to render your templates either from the controllers:
return $this->render(
'#foo_bar/template.html.twig',
$data
);
or from other twig templates
{% include '#foo_bar/template.html.twig' %}
Source: official cookbook http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html
To add a directory for twig templates I do this:
chdir(__DIR__);
$this->container->get('twig.loader')->addPath('../../../../web/templates/', $namespace = '__main__');
this allows me to put twig templates in a folder called 'templates' in the web folder and symfony 2.3 has no issues loading them.
The class responsible for loading twig templates is stored at the service id twig.loader, which by default is an instance of Symfony\Bundle\TwigBundle\Loader\FilesystemLoader, and the class for locating templates is stored at the service id templating.locator and by default is an instance of the class Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator (which itself is injected as the first parameter to twig.loader)
So, in theory, all you would need to do is write your own class that implements Symfony\Component\Config\FileLocatorInterface (or extends Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator) and then inform the configuration to use your class (which I was going to look up how to do this for you, but Symfony's websites are down right now)

Symfony2 templates/layouts

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.

symfony 2.0 bundles interworking

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).

Resources