English 中文(简体)
Making jquery tmpl without a lot of extra script tags
原标题:

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

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

最佳回答

You don t have to do that. You can (for example) keep your templates in a .js file (sort-of like a message catalog):

// 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 that in and then you can use the templates via

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

In other words, $.tmpl() lets you pass in the template body as the first parameter, and it can come from anywhere.

Alternatively, because it s a little messy to write long templates as Javascript string constants, you could pile up a bunch of text <script> tags in a single static HTML file. You could then AJAX that and drop it in a hidden <div> or something. That way you can still get the advantage of caching, and it might be easier to maintain the templates.

Of course, along those lines, you could maintain the templates separately as individual files, and glue them together at site build time according to any packaging scheme.

问题回答

You can store your templates in external files (and than fetch them via Ajax, for example).

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

"... for remote templates, you can get a template markup as a string using any AJAX call you want, or you can simply use a static script block pointing to a js file which defines the string. Then you should use jQuery.template to compile the template from the string, and go from there, rendering it using jQuery.tmpl"





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签