Question

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
 <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 which is different from {% and %} ? Thank you!

Was it helpful?

Solution

@jodator has a good approach.

Alternatively you can use PHP in your Volt template

<script id="template-upload" type="text/x-tmpl">
<?php foreach (.....) { ?>
   <tr class="template-upload fade">
   <td>
       <span class="preview"></span>
       ........//Some similar code here
   </td>
   </tr>
 <?php } ?>
 </script>

The only issue here is that you have to be careful on what the scope of your variables are so that PHP can process them. For instance if o.files is a javascript object then you need to pass it as a variable in PHP. If it is a PHP object then all you will have to do is change it to $o.files

OTHER TIPS

You can:

  1. Change javascript template syntax which this plugin uses.
  2. Use different templates in jQuery File Upload
  3. Echo strings which causes problems with volt ( "{%", "%}", "{{", "}}" ) :
<script id="template-upload" type="text/x-tmpl">
{{ '{%' }} for (var i=0, file; file=o.files[i]; i++) { {{ '%}' }}

(3) is kinda messy but should work.

You could change the template regex, per the Javascript Templates documentation:

To use different tags for the template syntax, override tmpl.regexp with a modified regular expression, by exchanging all occurrences of "{%" and "%}", e.g. with "[%" and "%]":

tmpl.regexp = /([\s'\\])(?!(?:[^[]|\[(?!%))*%\])|(?:\[%(=|#)([\s\S]+?)%\])|(\[%)|(%\])/g;

Check out the readme: https://github.com/blueimp/JavaScript-Templates#template-parsing

Try this code for jquery file upload plugin:

<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{{ '{%' }} for (var i=0, file; file=o.files[i]; i++) { {{ '%}' }}
<tr class="template-upload fade">
    <td>
        <span class="preview"></span>
    </td>
    <td>
        <p class="name">{{ "{%=" }} file.name {{ "%}" }}</p>
        <strong class="error text-danger"></strong>
    </td>
    <td>
        <p class="size">Processing...</p>
        <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
    </td>
    <td>
                {{ "{%" }} if (!i && !o.options.autoUpload) { {{ "%}" }}
            <button class="btn btn-primary start" disabled>
                <i class="glyphicon glyphicon-upload"></i>
                <span>Start</span>
            </button>
        {{ "{%" }} } {{ "%}" }}
    {{ "{%" }} if (!i) { {{ "%}" }}
            <button class="btn btn-warning cancel">
                <i class="glyphicon glyphicon-ban-circle"></i>
                <span>Cancel</span>
            </button>
    {{ "{%" }}  }  {{ "%}" }}
    </td>
</tr>
{{ '{%' }} } {{ '%}' }}
</script>

<script id="template-download" type="text/x-tmpl">
{{ "{%" }} for (var i=0, file; file=o.files[i]; i++) {  {{ "%}" }}
<tr class="template-download fade">
    <td>
        <span class="preview">
                    {{ "{%" }} if (file.thumbnailUrl) { {{ "%}" }}
                <a href="{{'{%'}}=file.url{{'%}'}}" title="{{'{%'}}=file.name{{'%}'}}" download="{{'{%'}}=file.name{{'%}'}}" data-gallery><img src="{{'{%'}}=file.thumbnailUrl{{'%}'}}"></a>
        {{ "{%" }} } {{ "%}" }}
        </span>
    </td>
    <td>
        <p class="name">
            {{ "{%" }} if (file.url) { {{ "%}"}}
                <a href="{{'{%='}}file.url{{'%}'}}" title="{{'{%='}}file.name{{'%}'}}" download="{{'{%='}}file.name{{'%}'}}" {{'{%='}}file.thumbnailUrl?'data-gallery':'' {{'%}'}}>{{'{%='}}file.name{{'%}'}}</a>
            {{ "{%"}} } else { {{ "%}"}}
                <span>{{ "{%="}}file.name{{ "%}"}}</span>
            {{ "{%"}} } {{ "%}"}}
        </p>
        {{ "{%"}} if (file.error) { {{ "%}"}}
            <div><span class="label label-danger">Error</span> {{"{%="}}file.error{{"%}"}}</div>
       {{ " {%"}} } {{ "%}"}}
    </td>
    <td>
        <span class="size">{{ "{%="}}o.formatFileSize(file.size){{ "%}"}}</span>
    </td>
    <td>
        {{ "{% if (file.deleteUrl) { %}"}}
            <button class="btn btn-danger delete" data-type="{{'{%='}}file.deleteType{{'%}'}}" data-url="{{'{%='}}file.deleteUrl{{'%}'}}" {{"{%"}} if (file.deleteWithCredentials) { {{ "%}" }} data-xhr-fields='{"withCredentials":true}' {{ "{%"}} } {{"%}" }}>
                <i class="glyphicon glyphicon-trash"></i>
                <span>Delete</span>
            </button>
            <input type="checkbox" name="delete" value="1" class="toggle">
        {{ "{% } else { %}"}}
            <button class="btn btn-warning cancel">
                <i class="glyphicon glyphicon-ban-circle"></i>
                <span>Cancel</span>
            </button>
        {{ "{% } %}"}}
    </td>
</tr>
{{ "{% } %}"}}
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top