简体   繁体   中英

How to Pass a variable from javaScript to twig

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 #}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM