Pass javascript variable to twig
22,906
Solution 1
for (varJS = 0; varJS < 5; varJS++) {
var url = '{{ path("lesson", {'id': 'article_id'}) }}';
url = url.replace("article_id", varJS);
text += '<a href='+ url +'>article</a>';
}
Note You should really look at the FOSJsRoutingBundle.
Solution 2
According to Chalasr's answer.
I've done this thrick using :
<script>
var your_js_variable = 5;
`{{ path("deletePFTaxeSpeciale",{'id' : 'idCarburant' }) }}`.replace('idCarburant', your_js_variable)
<script>
Author by
hassen zouari
Updated on July 09, 2022Comments
-
hassen zouari almost 2 years
I just want to pass a javascript variable to a twig path. Now i'm using this, but it doesn't work.
<p id="result"></p> <script> var text = ""; var i; for (varJS = 0; varJS < 5; varJS++) { text += "<a href='{{ path('lesson',{'id': varJS }) }}'>article</a>"; } document.getElementById("result").innerHTML = text; </script>
-
hassen zouari about 8 yearsThanks , that's really what i need :)
-
chalasr about 8 yearsGlad to help you @hassenzouari
-
hassen zouari about 8 yearsyou are really the best bro :)