Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to integrate javascript to .volt template engine #2292

Closed
piavgh opened this issue Apr 3, 2014 · 9 comments
Closed

How to integrate javascript to .volt template engine #2292

piavgh opened this issue Apr 3, 2014 · 9 comments
Labels
duplicate Duplicate issue. The duplicate issue is referenced in the comments

Comments

@piavgh
Copy link

piavgh commented Apr 3, 2014

I need to use http://blueimp.github.io/jQuery-File-Upload/ in my project which use framework PhalconPHP

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, with javascript code
 <tr class="template-upload fade">
  <td>
  <span class="preview"></span>
    ........//Some similar code here
  </td>
 </tr>
{% } %} //problem here too
</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 which is different from {% and %} ? If not, I think this should be a necessary feature. Thank you!

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@mruz
Copy link
Contributor

mruz commented Apr 4, 2014

Please look at Volt: Template Engine and do you really need php and volt For loop for this?

<ul>
{% for robot in robots %}
    <li>{{ robot['name'] }}</li>
{% endfor %}
</ul>

is just converting to:

<ul>
<?php foreach ($robots as $robot) { ?>
    <li><?php echo $robot['name']; ?></li>
<?php } ?>
</ul>

So, is it your volt syntax right?

{% for (var i=0, file; file=o.files[i]; i++) { %}
...
{% } %}

@jodator
Copy link

jodator commented Apr 4, 2014

@mruz I think that problem is in escaping Vot's delimiters in volt template.

I think that all the code inside <script> tag is for JavaScript template engine and should not be interpreted by Volt (or manually escaped by developer to be exact).

Answer to the same question on StackOverflow

@maxgalbu
Copy link
Contributor

maxgalbu commented Apr 4, 2014

@jodator it can't be that way. You might need to input an array into javascript like this:

<script type="text/javascript">
var array = {{ phparray | json_encode }};
</script>

Twig solves this by using the {% verbatim %} tag, see here for the discussion about it for volt: https://github.com/phalcon/cphalcon/issues/1253

@jodator
Copy link

jodator commented Apr 4, 2014

@maxgalbu Maybe I clearify my previous posts: I still think that anything inside <script> tag is a HTML/JavaScript code for a JavaScript template engine used by jquery-upload-plugin not a PHP (server side). It should be escaped on server side. It's not a problem with passing PHP array to the HTML but with escaping Volt's delimiters.

This feature is missing in current Volt implementation and I agree that #1253 could solve this:

<script id="template-upload" type="text/x-tmpl">
{% literal %}
{% for (var i=0, file; file=o.files[i]; i++) { %}
// the rest of JS/HTML
{% endliteral %}
</script>

@romanlex
Copy link

How I can fix this trouble( I have js template( Maybe I can include file to volt template without parse?

@bogutski
Copy link

bogutski commented Sep 6, 2016

{{ "{{#each sales}}" }} ...

@dschissler

This comment was marked as abuse.

@dschissler

This comment was marked as abuse.

@sergeyklay
Copy link
Contributor

sergeyklay commented Jun 8, 2017

@sergeyklay sergeyklay added duplicate Duplicate issue. The duplicate issue is referenced in the comments and removed New Feature Request labels Jun 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Duplicate issue. The duplicate issue is referenced in the comments
Projects
None yet
Development

No branches or pull requests

9 participants