19

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it. The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.

How do I do so?

3
  • You can only do this using a new request. If you don't want to load the whole page, have a look at Ajax. Commented Jul 30, 2012 at 8:01
  • 1
    You should extend your question with code examples and specify places, where you want pass what Commented Jul 30, 2012 at 8:04
  • @Sgoettschkes, I want to reload the whole page with new data array, how do I do that? Commented Jul 30, 2012 at 8:49

3 Answers 3

41

Here are a few different ways:

{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}

or

{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}

or

{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}
Sign up to request clarification or add additional context in comments.

4 Comments

could you explain briefly what the only at the end of the include is for please?
Nevermind, I've found a good explaination here : stackoverflow.com/q/9935551/4074148
Ahh love between mans :) I've found a bug trying to do: {# any for loop #} {{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": forloop.var } ) }} {# endfor #} Even that the different values get inside the rendered controller, in the final template, they do not change. I'm tearing my white hairs out with this one.
Note that {% render %} is now deprecated, it is replaced by {{ render() }}.
33

Symfony 2.1:

{% render 'YourBundle:YourController:yourAction' with {'var': value} %}

Symfony 2.6+:

{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}

And, of course, read the documentation.

3 Comments

Variable "select1" does not exist in
That is deprecated, replaced with {{ render(controller('SomeBundle:SomeController:someAction', { 'someVariable': someValue })) }}.
As @Oltarus suggest, the render function needs parentheses: render( controller (...) )
1

I think some parts are depricated here. To make the include work in latest Symfony 3.1.10, I solved it like this:

{% extends 'base.html.twig' %}
{% block body %}
    {{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}

Note: include() with parentheses. Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.