-1

I want to get the variable from javaScript to set in twig variable. For exemple I have a variable "myId" in javaScript and get it to my twig varible.

var myid = 823;

My twig:

{% set = "(myId from JavaScript)" %} {# 823 #}
nyaina
  • 21
  • 4
  • 1
    Does this answer your question? [Accessing javascript variable inside {{ }} symfony twig](https://stackoverflow.com/questions/43373097/accessing-javascript-variable-inside-symfony-twig) – jeremy-denis Jun 22 '22 at 08:52
  • 1
    [What is the difference between client-side and server-side programming](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – DarkBee Jun 22 '22 at 08:59
  • I want to use the myid in JS to my twig variable – nyaina Jun 22 '22 at 09:26

1 Answers1

-1

Sorry, this won't work (at least out of the box).

Twig templates are rendered serverside without knowledge about Javascript variables, and the javascriptcode is executed on the client after the rendered template has been delivered to the client.

One way to solve this would be with aditional Javascript, render a span with an id in the twig template and update the html content of the span from JavaScript.

Morphine
  • 158
  • 1
  • 12
  • There is no need to answer an obvious duplicate question. Just close-vote the question instead of creating an identical answer provided in the marked question – DarkBee Jun 22 '22 at 12:47
  • OK got it, thanks. Didnt find the duplicate post in the first place (followed the link you posted after i wrote the answer) and don't have close vote priv, so obviously my fault, sorry. – Morphine Jun 22 '22 at 14:15