solook
solook

Reputation: 1

How to collect data from Twig?

I have for example in Twig:

{% customTag %}city1{% endCustomTag %}
{% customTag %}city2{% endCustomTag %}

or:

customFunction('city1')
customFunction('city2')

or:

 {{ 'city1' | customFilter }}
 {{ 'city2' | customFilter }}

And how to collect data from tag/function/filter for example in controller? I would like to receive in controller "city1" and "city2".

I know how to create custom tag, function and filter but is any way to get all data from these elements in controller or service?

Upvotes: 0

Views: 340

Answers (1)

Dan Costinel
Dan Costinel

Reputation: 1736

I'm not really sure what you want, but I'll give it a try. To send some data from twig template into a Controller, I would put an anchor inside your custom tags, and use path function, and then just add the extra info. Like:

#let's say your controller method where you want to send data looks like
/**
 * @Route("/test", name="test")
 */
public function testAction(Request $request) {
    $param1 = $request->get('param1'); #will receive 'city1'
    $param2 = $request->get('param2'); #will receive 'city2'
}

#twig
{% customTag %}<a href="{{ path('test', { 'param1': 'city1', 'param2': 'city2'}) }}" style="pointer-events: none;">cities</a>{% endCustomTag %}

Pay attention that the extra info is sent from Twig into Controller using $_GET.

Upvotes: 1

Related Questions