Skip to content
Advertisement

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>

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement