2

I have a small problem with a Javascript variable in a Twig template, what I want is to pass a variable as a parameter (in a url) to a function. All I have found on StackOverflow is how to pass a string 'blablabla' to the URL like:

url/{param}, {'param' : 'blablabla'}

but I want something like :

$var =...;
url/{param}, {'param': $var}

this photo should make it clear to you , thanks for reading

photo

3
  • 1
    Can't be done that way. Twig is rendered on the server. You need to use ajax or fosrouting Commented Aug 6, 2017 at 12:04
  • Thanks Man , any useful links for ajax or fosrouting to solve this issue ?? Commented Aug 6, 2017 at 12:19
  • Click on the blue word fosrouting. Is a "How To" Commented Aug 6, 2017 at 13:08

1 Answer 1

2

I advise you to use the FOS JsRouting bundle. It is totally suited to what you want to do.

Here is the official documentation : https://symfony.com/doc/current/bundles/FOSJsRoutingBundle/usage.html

Example in Twig :

Routing.generate('your_route_with_params', { param: varJavascript });

Enjoy

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , I will read the documentation carefully and I hope I can get what I want

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.