Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass javascript variable to twig

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>
like image 560
hassen zouari Avatar asked Mar 29 '16 17:03

hassen zouari


2 Answers

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.

like image 142
chalasr Avatar answered Oct 07 '22 21:10

chalasr


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>
like image 40
Hicham O-Sfh Avatar answered Oct 07 '22 21:10

Hicham O-Sfh