Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to integrate javascript code in volt template

I need to use Jquery File Uploader in my project

In order to do so, my .volt file need to contain a javascript code like this ```<script id="template-upload" type="text/x-tmpl"> {% for (var i=0, file; file=o.files[i]; i++) { %} //The problem begin here <tr class="template-upload fade"> <td> <span class="preview"></span> ........//Some similar code here </td> </tr> {% } %} </script>```

But the problem is {% and %} is .volt template syntax. When I use {% for (var i=0, file; file=o.files[i]; i++) { %} like that, the .volt syntax and javascript syntax are conflict. Browser such as Chrome or Firefox will show the error : "Syntax error, unexpected token ( in /var/www/....) on line 77" where 77 is that line start with {%

In .phtml it works fine, but I don't want to rebuild my whole view template with .phtml How can I use this code with .volt? Is there other syntax for javascript ? Thank you!



48.2k
Accepted
answer

I've encountered this problem with Mustache templates. There, the solution was to simply change the delimiters in Mustache. To the best of my knowledge, Volt doesn't have the ability to change it's delimiters.