Example 2: Dynamic list

Compile html files to js

1
2
3
4
$ jqfy -i templates/ -o templates.js --fix-return-type
compile templates/item.html
compile templates/list.html

By jqfy command we compile html templates to templates.js

Create new list

index.html
1
2
3
4
5
6
7
8
<script src="jquery.min.js"></script>
<script src="templates.js"></script>
<script>
$(function () {
templates.list().appendTo('#example');
});
</script>
<div id="example"></div>

Result

Now we have an input and button. by clicking on the button, a list item with text of input will be added.

We have two html templates. templates/list.html and templates/item.html.

html templates

templates/list.html download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div class="panel panel-default">
<div class="panel-body">
<div class="input-group">
<input type="text" class="form-control" jqfy:name="$textInput">
<span class="input-group-btn">
<button class="btn btn-success" type="button" jqfy:name="$addBtn">
add
</button>
</span>
</div>
</div>
<ul class="list-group" jqfy:name="$list"></ul>
</div>
<script>
$addBtn.on('click', function (e) {
var text = $textInput.val(); // get input value
$textInput.val(''); // empty input
templates
.item({text: text}) // render new item
.appendTo($list); // append item to list
});
</script>
templates/item.html download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<li class="list-group-item">
<button class="btn btn-danger btn-xs stick-top stick-right" type="button" jqfy:name="$removeBtn">
<span class="glyphicon glyphicon-remove"></span>
</button>
<span jqfy:name="$textSpan">{{ text }}</span>
<div class="clearfix"></div>
</li>
<script>
if (data.text.trim() == '') { // if text is empty
$textSpan.html('&nbsp;'); // prevent collapse
}
$removeBtn.on('click', function () {
output.remove(); // remove item when remove btn clicked
});
</script>

Generated template.js

templates/item.html download
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
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define("templates", ['jquery'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('jquery'));
} else {
root.templates = factory(jQuery);
}
})(this, function($) {
var templates = {};
templates.item = function (data, opts) {
// generated by jQfy 1.3.7
opts = $.extend({}, opts);
data = $.extend({}, data);
var $root = $('<div/>');
var $li1 = $('<li/>')
.addClass("list-group-item")
.appendTo($root);
var $removeBtn = $('<button/>')
.addClass("btn btn-danger btn-xs stick-top stick-right")
.attr("type", "button")
.appendTo($li1);
var $span1 = $('<span/>')
.addClass("glyphicon glyphicon-remove")
.appendTo($removeBtn);
// end $span1
// end $removeBtn
var $textSpan = $('<span/>')
.appendTo($li1);
$textSpan.append(document.createTextNode("" + (data.text == undefined ? "" : data.text) + ""));
// end $textSpan
var $div1 = $('<div/>')
.addClass("clearfix")
.appendTo($li1);
// end $div1
// end $li1
// start script 1
if (data.text.trim() == '') { // if text is empty
$textSpan.html('&nbsp;'); // prevent collapse
}
$removeBtn.on('click', function () {
output.remove(); // remove item when remove btn clicked
});
// end script 1
// end $root
var output = $root.contents();
return output;
};
templates.list = function (data, opts) {
// generated by jQfy 1.3.7
opts = $.extend({}, opts);
data = $.extend({}, data);
var $root = $('<div/>');
var $div1 = $('<div/>')
.addClass("panel panel-default")
.appendTo($root);
var $div2 = $('<div/>')
.addClass("panel-body")
.appendTo($div1);
var $div3 = $('<div/>')
.addClass("input-group")
.appendTo($div2);
var $textInput = $('<input/>')
.addClass("form-control")
.attr("type", "text")
.appendTo($div3);
// end $textInput
var $span1 = $('<span/>')
.addClass("input-group-btn")
.appendTo($div3);
var $addBtn = $('<button/>')
.addClass("btn btn-success")
.attr("type", "button")
.appendTo($span1);
$addBtn.append(document.createTextNode("add"));
// end $addBtn
// end $span1
// end $div3
// end $div2
var $list = $('<ul/>')
.addClass("list-group")
.appendTo($div1);
// end $list
// end $div1
// start script 1
$addBtn.on('click', function (e) {
var text = $textInput.val(); // get input value
$textInput.val(''); // empty input
templates
.item({text: text}) // render new item
.appendTo($list); // append item to list
});
// end script 1
// end $root
var output = $root.contents();
return output;
};
return templates;
});

And some extra css

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
.stick-right {
float: right;
margin-right: -15px;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.stick-top {
margin-top: -10px;
border-top-right-radius: 0;
border-top-left-radius: 0;
}
.stick-left {
float: left;
margin-left: -15px;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
#example {
margin: 10px;
max-width: 350px;
margin-left: auto;
margin-right: auto;
}
body{
background-color: #EDE7F6;
}