我需要在我的项目中使用框架PhalconPHP的 http://blueimp.github.io/jQuery-File-Upload/
为了做到这一点,我的.volt文件需要包含这样的javascript代码
<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>
但问题是 {% 和 %} 是 .volt 模板语法。当我使用这样的{% for (var i=0, file; file=o.files[i]; i++) { %}
时,.volt语法和javascript语法是冲突的。Chrome 或 Firefox 等浏览器将显示错误:"语法错误,意外令牌(在/var/www/....在第 77 行"上,其中 77 是以 {% 开头的那行
中它工作正常,但我不想用.phtml重建我的整个视图模板如何将此代码与 .volt 一起使用?JavaScript 是否有其他语法与 {% 和 %} 不同?谢谢!
@jodator有一个很好的方法。
或者,您可以在Volt模板中使用PHP
。<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>
这里唯一的问题是你必须小心变量的范围,以便PHP可以处理它们。例如,如果o.files
是一个javascript对象,那么你需要在PHP中将其作为变量传递。如果它是一个PHP对象,那么你所要做的就是将其更改为$o.files
您可以:
- 更改此插件使用的 JavaScript 模板语法。
- 在 jQuery 文件上传中使用不同的模板
- 导致伏特(
"{%", "%}", "{{", "}}"
)问题的回声弦:
<script id="template-upload" type="text/x-tmpl">
{{ '{%' }} for (var i=0, file; file=o.files[i]; i++) { {{ '%}' }}
(3)有点乱,但应该可以工作。
您可以根据 Javascript 模板文档更改模板正则表达式:
要对模板语法使用不同的标签,请使用修改后的正则表达式覆盖 tmpl.regexp,方法是交换所有出现的"{%"和"%}",例如与"[%"和"%]"交换:
tmpl.regexp = /(['s'''])(?!(?:[^[]|'[(?!%))*%'])|(?:'[%(=|#)(['s'S]+?)%'])|('[%)|(%'])/g;
查看自述文件:https://github.com/blueimp/JavaScript-Templates#template-parsing
为 jquery 文件上传插件尝试以下代码:
<!-- 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>