-
Notifications
You must be signed in to change notification settings - Fork 2
/
subtemplate.html
135 lines (97 loc) · 4.57 KB
/
subtemplate.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>jQuery.template</title>
<script src="http://assets.dev/js/jquery/jquery-1.4.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="http://assets.dev/js/jquery/plugins/template/jquery.template.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!-- Applying this template... -->
<script type="text/html" id="subtemplate">
{% /* this is proper markup for a template comment */ %}
<!-- HTML comments are inserted into the DOM -->
{% /* argument passed to $.template is accessed as "this" */ %}
<h1>{%= this.name %}</h1>
{% /* functions act as subtemplates */ %}
{%
/** NOTE!
* subtemplates can be declared anywhere inside a template
* if you use:
* function subtemplate () {}
* but not if you use:
* var subtemplate = function () {};
*/
%}
{% function renderJob (job) { %}
{% /* variables declared in a subtemplate are local to the subtemplate */ %}
{% var verb = (job.end !== 'present') ? 'worked' : 'currently work'; %}
I {%= verb %} at <strong>{%= job.company %}</strong> as a <em>{%= job.position %}</em> {% renderDateRange(job.start, job.end) %}
{% /* subtemplates can be nested (nested subtemplates are private) */ %}
{% function renderDateRange (start, end) { %}
{% /* you can print with ternaries */ %}
from {%= start %} {%= end === 'present' ? '' : 'to ' + end %}.
{% } %}
{% } %}
<ul>
{% /* global variables are accessible inside template (ex. jQuery) */ %}
{% jQuery.each(this.jobs, function (i, job) { %}
{% /* execute ({% ... %}), don't print ({%= ... %}), subtemplates */ %}
<li>{% renderJob(job) %}</li>
{% }); %}
</ul>
</script>
<!-- ...to this data... -->
<script type="text/javascript" charset="utf-8">
$(function () {
var tpl = $('#subtemplate').template();
tpl({
name: 'Dave Furfero',
jobs: [
{company:'MLB.com', position:'User Interface Engineer', start:'March 2009', end:'present'},
{company:'Time, Inc.', position:'Front-end Developer', start:'July 2008', end:'March 2009'},
{company:'Live Nation', position:'Director of Product Development', start:'February 2008', end:'July 2008'},
{company:'Live Nation', position:'Senior Product Developer', start:'August 2007', end:'February 2008'},
{company:'Owl Music Search', position:'Interface Design Director', start:'April 2006', end:'June 2007'}
]
}).appendTo('#content');
});
</script>
<!-- ...creates this function (edited for clarity)... -->
<script type="text/javascript" charset="utf-8">
(function anonymous () {
var out = [];
out.push(" <!-- HTML comments are inserted into the DOM --> ");
out.push(" <h1>", this.name, "</h1> ");
function renderJob(job) {
var verb = job.end !== "present" ? "worked" : "currently work";
out.push(" I ", verb, " at <strong>", job.company, "</strong> as a <em>", job.position, "</em> ");
renderDateRange(job.start, job.end);
function renderDateRange(start, end) { out.push(" ");
out.push(" from ", start, " ", end === "present" ? "" : "to " + end, ". ");
}
}
out.push(" <ul> ");
jQuery.each(this.jobs, function (i, job) {
out.push(" <li>");
renderJob(job);
out.push("</li> ");
});
out.push(" </ul> ");
return out.join("");
})
</script>
<!-- ...renders this markup... -->
<!-- HTML comments are inserted into the DOM -->
<h1>Dave Furfero</h1>
<ul>
<li>I currently work at <strong>MLB.com</strong> as a <em>User Interface Engineer</em> from March 2009 .</li>
<li>I worked at <strong>Time, Inc.</strong> as a <em>Front-end Developer</em from July 2008 to March 2009.</li>
<li>I worked at <strong>Live Nation</strong> as a <em>Director of Product Development</em> from February 2008 to July 2008.</li>
<li>I worked at <strong>Live Nation</strong> as a <em>Senior Product Developer</em> from August 2007 to February 2008.</li>
<li>I worked at <strong>Owl Music Search</strong> as a <em>Interface Design Director</em> from April 2006 to June 2007.</li>
</ul>
<!-- ...into this element. -->
<div id="content"></div>
</body>
</html>