Creating jQuery tmpl without a lot of script tags

I read about the jQuery template plugin http://api.jquery.com/tmpl/ as a means of binding data to markup. All examples show that you define a template in the script tag of a new script. I would prefer not to clutter my html with <head>all script tags.

Is there a better way to organize this? Maybe with a templates folder full of scripts that load dynamically?

+3
source share
2 answers

You do not have to do this. You can (for example) save your templates in a .js file (sort of like a message directory):

// this is a file full of templates
var SITE_TEMPLATES = {
  ERROR_1: 'This is an error template: the error is ${error.msg}',
  WHATEVER: 'I cannot make this stuff up but you get the ${idea}'
};

Pull this and then you can use patterns through

$('#someplace').append($.tmpl(SITE_TEMPLATES.ERROR_1, { error: { msg: "hello world" }}));

, $.tmpl() , .

, Javascript , <script> HTML . AJAX <div> - . , , .

, .

+2

(, , Ajax).

http://api.jquery.com/jQuery.template/

"... , AJAX, , script, js, . jQuery.template , jQuery.tmpl"

+2

Source: https://habr.com/ru/post/1771743/


All Articles