-
Notifications
You must be signed in to change notification settings - Fork 6
/
README
68 lines (35 loc) · 1.87 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
jQuery Templates
================================================================================
A jQuery templating engine that allows embedded javascript templates for easy client side UI processing.
see http://github.com/merblogger for real application usage examples.
To load all templates:
Usually this is done when the document is ready, for example:
$(document).ready(function() {
$.loadTemplates();
});
Templates are loaded in the DOM as <script/> tags and the rules are simple. Set the attribute to 'type="text/x-jquery-template"' and the title to what you would like to name the template. You will use the name to render the template later.
Let's consider a simple example of a template to generate a select box for entities:
<script type="text/x-jquery-template" title="entities">
<select>
<option value="">[new]</option>
<% $.each(self.entities, function(index, entity) { %>
<option value="<%= entity.value %>"> <%= entity.name %> </option>
<% }); %>
</select>
</script>
$.loadTemplates() 'compiles' the template with title 'entities' into the $.templates object and is directly accessable as $.templates.entites. Templates are compiled into JS functions that you can call passing in template 'local' data. For examle:
var html = $.templates.entites(
[{value: 1, name: "foo"}, {value: 2, name: "bar" }]
);
After running this the 'html' variable will contain:
<select>
<option value="">[new]</option>
<option value="1"> foo </option>
<option value="2"> bar </option>
</select>
Using jQuery this can be inserted directly into the DOM:
$("body").append(html);
OR
html.appendTo($("body"));
Helper methods may also be added to the templater, currently it comes with a 'partial' helper available. The 'partial()' helper allows us to call a named template from within another template.
partial("other-partial-title", ...local data...);