rendering controller action in symfony twig - symfony

Updating deprecated code and i have following problem with render tag
> {% render url('_internal_main_navigation', {}) %}
transaltes to
{{ render(controller('MyBundle::menu', {})) }}
Twig does this thing where it appends "Controller" and "Action" to the specified route so That should call MyBundle/Controller/Controller->menuAction() but apparently the Controller class is supposed to have some sort of prefix like "MyController" so the twig route can be specified like 'Bundle:My:menu'
otherwise i get
Class "MyBundle" noes not exist
Can i make it work without prefixing the controller?

I'd recommend to use render_esi() which supports routes via url() by name aswell and you're set for ESI in the future.
When using the default render() function (or setting the renderer to
inline), Symfony merges the included page content into the main one
before sending the response to the client. But if you use the esi
renderer (i.e. call render_esi()) and if Symfony detects that it's
talking to a gateway cache that supports ESI, it generates an ESI
include tag. But if there is no gateway cache or if it does not
support ESI, Symfony will just merge the included page content within
the main one as it would have done if you had used render().
See Using ESI in Symfony
The following syntax will work in your case:
{{ render_esi(url('_internal_main_navigation', {})) }}
If you'd like to render by controller-name, you could turn your controller into a service and refer to it by the name of the service (i.e. mybundle_controller):
{{ render_esi(controller('mybundle_controller:menuAction', {})) }}

Related

Twig how to get html between two tags to a service

I want some simple twig tags that allow me to do the following:
{% customtag 'name' %}
<div> some html </div>
{% endcustomtag %}
And then get that html inside of a service.
I have tried doing this myself but when I finally have al the data that I want inside of my NodeVisitor I can't seem to get it to my service. If I inject it and call a method on it it never gets executed. It only gets called if I try to clear my cache from my command line.
Can somebody please give some insight?
Apparently, you can access your extensions from Twig_Template.
So you can do:
$compiler->write('$this->extensions[')
->string('your_extension')
->write(']->getService()->someFunction();')
->raw(PHP_EOL);
in your twig node. And then in your extension you should just inject the service and return it in the getService method.

Can't use render(controller) in SonataAdminBundle template

I create custom controller(extending Sonata\AdminBundle\Controller\CRUDController) and action.
When render this action {{render(controller('MainBundle:SonataAdmin/Order:searchCertificate'))}}
I get Symfony error:
An exception has been thrown during the rendering of a template ("There is no _sonata_admin defined for the controller MainBundle\Controller\SonataAdmin\OrderController and the current route").
I found answer in official documentation:
If you want to render a custom controller action in a template by
using the render function in twig you need to add _sonata_admin as an
attribute. For example; {{
render(controller('AppBundle:XxxxCRUD:comment', {'_sonata_admin':
'sonata.admin.xxxx' })) }}. This has to be done because the moment the
rendering should happen the routing, which usually sets the value of
this parameter, is not involved at all, and then you will get an error
"There is no _sonata_admin defined for the controller
AppBundleControllerXxxxCRUDController and the current route ' '."
i have solved this Problem by setting the _sonata_admin in the comming request:
with normal Controller:
$request->request->set('_sonata_admin','admin.template');

How to render only one field of a form with symfony

I want to render only one field of a form. When i put {{form_end(form)}} every other field are coming (symfony doc show it clearly) but how to render only one field ? If i dont put {{form_end(form)}}, there is only one field, but no save button
thanks
Yes, CSS can do the trick. But do you want the working of your application to depend on client side styling rules? In most cases it might beter not to render the field HTML at all.
There are two ways in which you can fix this in your template.
Put {% do form.field_you_want_to_hide.setRendered %} before your {{form_end(form)}}.
This will mark the field as rendered and thus it will not show up when form_rest is called.
Instead of {{form_end(form)}}, use {{ form_end(form, {'render_rest': false}) }}, as explained in the Symfony Twig documentation.
It would be even better to change your form class such that the fields are removed from your form. Is it your own form you would like to render, or a form from a third party bundle?

Call Symfony Service on demand from twig

I've created a twig template that will create a table of items with checkboxes and a js function that can be triggered using a button that will return the IDs of all items where the checkboxes are checked. This works fine so far. Now I need to call a service and pass an array of all selected IDs.
Is there a good way to call the service from within the js part in the twig template? I don't want to create a controller for the service and use curl to call it.
Best regards Christian
# app/config/config.yml
twig:
globals:
myService: "#my.service"
And in twig you can use now:
{{ myService.anyMethod() }}
You can also make Twig extension:
http://symfony.com/doc/current/cookbook/templating/twig_extension.html

Creating menu on symfony2.3 using render

on my Admin template I've used render function to add the menu.
The controller sidebar add all links from db.
The problem is made when i want to add "current" class because i can't access of current url/controller from a render request.
{{ render(controller('AdminDashboardBundle:Template:sidebar')) }}
How I can access to all informations from the render controller (without pass a var )?
Thanks
The RequestStack service has been built with Symfony 2.4. If you declare your Template controller as a service and inject RequestStack, you'll be able to use your current render call without passing arguments.
But you are speaking about Symfony 2.3, and unfortunately I don't think it is possible to do what you want without arguments. Here are some example on how to pass the current route / the URL as an argument of your controller.
1) Passing the URL :
{{
render(controller('AdminDashboardBundle:Template:sidebar', {
'url': app.request.requesturi
}))
}}
2) Passing the route :
{{
render(controller('AdminDashboardBundle:Template:sidebar', {
'route': app.request.attributes.get('_route'),
'route_params': app.request.attributes.get('_route_params')
}))
}}
I know you want to use Symfony2.3 and this call without passing vars, I think that's simply not possible because of how work scopes.

Resources