javascript - js 内的 html 与 twig 模板冲突标签

标签 javascript html twig slim

我正在尝试制作一个具有多个文件上传的简单项目。我创建了 UploadHandler.php 来处理上传。然后在 View 中包含脚本和 html,如下所示。我正在使用 twig 模板和 slim 框架。我是新手,遇到一些语法问题。

<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>
<!-- The template to display files available for download -->
<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>
{% } %}
{% endblock %}

第期:

Message: Unexpected character ";" in "uploads/list.twig" at line 67 which is the second line. Anyone that can help me?

最佳答案

你的例子绝对正确。问题是使用了js模板语言,问题出在twig格式和text/x-tmpl之间的冲突。

我建议将 text/x-tmpl block 移动到单独的 .js 文件中,并将其包含在 Twig 中作为引用。

我在这里找到了更多详细信息 What is x-tmpl?

更新: 使用单独的 .js 文件的解决方案不起作用。但还有另一种解决方案 - 使用包含标签的脚本创建一个单独的 html 文件(uploadables-js.html)。 在 .twig 文件中插入指向此新文件的链接,如下所示 {{ source('uploadables-js.html') }}。重要提示:包含或使用将不起作用

关于javascript - js 内的 html 与 twig 模板冲突标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156190/

相关文章:

javascript - 选中/取消选中 radio

javascript - CKEDITOR 不会同时保存/解析属性和样式中的宽度/高度

php - 使用 Symfony Forms、Twig 和 SymfonyTwigBridge 时如何覆盖默认模板?

javascript - 在这种情况下如何使用 bluebird Promise?

javascript - 正则表达式Javascript匹配除一个之外的字母

javascript - this.method = function(){} VS obj.prototype.method = function (){}

html - 将三 Angular 形与文本居中对齐

php - 在多选框中选择的所有值都没有记录在 MySQL 数据库中

php - Symfony 和 Twig 模板。如何动态扩展模板或什么也不扩展?

php - 如何屏蔽超过 4 位数字的年份?