10

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>
2
  • do you mean variable value here? Commented Mar 29, 2016 at 17:43
  • yeah , the varJS vallue Commented Mar 29, 2016 at 17:53

2 Answers 2

23
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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , that's really what i need :)
Glad to help you @hassenzouari
you are really the best bro :)
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.