blueimp/JavaScript-Templates

Templates as an external file when using them in client-side

Closed this issue · 2 comments

I read the documentation on the project home page and tried to implement it on a project of mine and it worked as expected.

Although having the template inside a script tag inline in the HTML is something I would like to avoid.

I tried moving the template to an external file and reference it with the src attribute but it didn't work.

<script type="text/x-tmpl" id="template" src="template.js"></script>

I even forced the headers sent by template.js but no luck either.

Why is it that reference it as an external file doesn't work? Is there a way to have the template external to the HTML code?

Right now I by-pass this issue including them with PHP, but most of all I would like to know why it fails as an external file in the script tag.

The code inside of the templates script tag is not valid JavaScript, therefore it won't work when referenced from an external script source.

However you can compile your template files and then reference the result JavaScript file as explained here:
https://github.com/blueimp/JavaScript-Templates#compiled-templates

I know it's not valid JS, but it puzzles me that inline it is ignored, but not when it's external even when a bogus type was specified, and if the content-type header is bogus as well. Then I guess that means the script tag always treats external files as JS no matter the types.

Thanks for your quick answer.