Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is x-tmpl?

Tags:

html

Was looking for the answer on the net, but couldn't find anything. This little snack of code really frustrates me, as I can't understand it. (It's part of the plugin called: jQuery File Upload)

<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
  <tr class="template-download fade">
    {% if (file.error) { %}
        <td></td>
        <td class="name"><span>{%=file.name%}</span></td>
        <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
        <td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
    {% } else { %}
        <td class="preview">{% if (file.thumbnail_url) { %}
            <a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
        {% } %}</td>
        <td class="name">
            <a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
        </td>
        <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
        <td colspan="2"></td>
    {% } %}
    <td class="delete">
        <button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
            <i class="icon-trash icon-white"></i>
            <span>{%=locale.fileupload.destroy%}</span>
        </button>
        <input type="checkbox" name="delete" value="1">
    </td>
  </tr>
{% } %}
</script>
  • So, what type is text/x-tmpl?
  • What are these {% and %} tags?
  • What parser executes this code?
  • ...

...but would like to know everything about this.

like image 584
Dyin Avatar asked Apr 05 '12 09:04

Dyin


3 Answers

x-tmpl has no real meaning, it simply stops the browser from interpreting the script as javascript.

It's mostly used with jquery templates or knockoutjs template binding.

At some point, a javascript data object will be used in conjunction with the template to render some html. The values in the data object will replace those marked with {%} and similar in the template, with some {%} sections reflecting code flow such as loops etc.

I'm not sure which templating library uses {%} however, it's not jquery templates, jsrender or knockout. You'd have to check what libraries are being referenced in the sample code.

like image 147
NibblyPig Avatar answered Nov 14 '22 06:11

NibblyPig


So, what type is text/x-tmpl?

A non-standard one. Looks like a template though.

What are these {% and %} tags?

Part of the template language.

What parser executes this code?

Probably one written in JavaScript and imported in another <script> element on the same page.

like image 29
Quentin Avatar answered Nov 14 '22 04:11

Quentin


jQuery-File-Upload has a dependency on JavaScript-Templates which uses the nonstandard text/x-tmpl mime type

  • https://github.com/blueimp/jQuery-File-Upload
  • https://github.com/blueimp/JavaScript-Templates
like image 1
Shai Coleman Avatar answered Nov 14 '22 06:11

Shai Coleman