Skip to content

Commit 6ad43cf

Browse files
committed
Print out falsy values. Closes blueimp#26.
1 parent 96c33a5 commit 6ad43cf

File tree

10 files changed

+41
-44
lines changed

10 files changed

+41
-44
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.PHONY: js
22

33
js:
4-
uglifyjs -nc tmpl.js > tmpl.min.js
4+
node_modules/.bin/uglifyjs tmpl.js -c -m -o tmpl.min.js

README.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# JavaScript Templates
22

33
## Demo
4-
[JavaScript Templates Demo](http://blueimp.github.com/JavaScript-Templates/)
4+
[JavaScript Templates Demo](http://blueimp.github.io/JavaScript-Templates/)
55

66
## Usage
77

@@ -271,14 +271,6 @@ Use dot notation to print nested properties:
271271
<strong>{%=o.author.name%}</strong>
272272
```
273273

274-
Note that the JavaScript Templates engine prints **falsy** values as empty strings.
275-
That is, **undefined**, **null**, **false**, **0** and **NaN** will all be converted to **''**.
276-
To be able to print e.g. the number 0, convert it to a String before using it as an output variable:
277-
278-
```html
279-
<h3>{%=0+''%}</h3>
280-
```
281-
282274
### Evaluation
283275
Use **print(str)** to add escaped content to the output:
284276

index.html

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE HTML>
22
<!--
33
/*
4-
* JavaScript Templates Demo 2.1.0
4+
* JavaScript Templates Demo 2.2.0
55
* https://github.com/blueimp/JavaScript-Templates
66
*
77
* Copyright 2011, Sebastian Tschan
@@ -13,16 +13,19 @@
1313
-->
1414
<html lang="en">
1515
<head>
16-
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><![endif]-->
16+
<!--[if IE]>
17+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
18+
<![endif]-->
1719
<meta charset="utf-8">
1820
<title>JavaScript Templates Demo</title>
1921
<meta name="description" content="&lt; 1KB lightweight, fast &amp; powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.">
20-
<meta name="viewport" content="width=device-width">
21-
<link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap.min.css">
22+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
23+
<link rel="stylesheet" href="http://blueimp.github.io/cdn/css/bootstrap.min.css">
2224
<style>body{padding-top:60px;}</style>
23-
<link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap-responsive.min.css">
24-
<!--[if lt IE 7]><link rel="stylesheet" href="http://blueimp.github.com/cdn/css/bootstrap-ie6.min.css"><![endif]-->
25-
<!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
25+
<link rel="stylesheet" href="http://blueimp.github.io/cdn/css/bootstrap-responsive.min.css">
26+
<!--[if lt IE 7]>
27+
<link rel="stylesheet" href="http://blueimp.github.io/cdn/css/bootstrap-ie6.min.css">
28+
<![endif]-->
2629
</head>
2730
<body>
2831
<div class="navbar navbar-fixed-top">
@@ -36,8 +39,8 @@
3639
<a class="brand" href="https://github.com/blueimp/JavaScript-Templates">JavaScript Templates</a>
3740
<div class="nav-collapse">
3841
<ul class="nav">
39-
<li class="active"><a href="#">Demo</a></li>
40-
<li><a href="https://github.com/blueimp/JavaScript-Templates/downloads">Downloads</a></li>
42+
<li class="active"><a href="http://blueimp.github.io/JavaScript-Templates/">Demo</a></li>
43+
<li><a href="https://github.com/blueimp/JavaScript-Templates/tags">Downloads</a></li>
4144
<li><a href="https://github.com/blueimp/JavaScript-Templates">Source Code</a></li>
4245
<li><a href="https://github.com/blueimp/JavaScript-Templates">Documentation</a></li>
4346
<li><a href="https://github.com/blueimp/JavaScript-Templates/issues">Issues</a></li>
@@ -108,7 +111,7 @@ <h4>Features</h4>
108111
<script src="tmpl.min.js"></script>
109112
<!-- jQuery and Bootstrap JS are not required, but included for the demo -->
110113
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
111-
<script src="http://blueimp.github.com/cdn/js/bootstrap.min.js"></script>
114+
<script src="http://blueimp.github.io/cdn/js/bootstrap.min.js"></script>
112115
<script>
113116
/*global window, jQuery, tmpl */
114117
(function ($) {

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blueimp-tmpl",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"title": "JavaScript Templates",
55
"description": "< 1KB lightweight, fast & powerful JavaScript templating engine with zero dependencies. Compatible with server-side environments like node.js, module loaders like RequireJS and all web browsers.",
66
"keywords": [
@@ -31,9 +31,9 @@
3131
}
3232
],
3333
"devDependencies": {
34-
"mocha": "latest",
35-
"expect.js": "latest",
36-
"uglify-js": "latest"
34+
"mocha": "1.11.0",
35+
"expect.js": "0.2.0",
36+
"uglify-js": "2.3.6"
3737
},
3838
"scripts": {
3939
"test": "mocha --reporter spec"

runtime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates Runtime 2.1.0
2+
* JavaScript Templates Runtime 2.2.0
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -29,7 +29,7 @@
2929
"'" : "&#39;"
3030
};
3131
tmpl.encode = function (s) {
32-
return String(s || "").replace(
32+
return String(s).replace(
3333
tmpl.encReg,
3434
function (c) {
3535
return tmpl.encMap[c] || "";

test/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE HTML>
22
<!--
33
/*
4-
* JavaScript Templates Test 2.1.0
4+
* JavaScript Templates Test 2.2.0
55
* https://github.com/blueimp/JavaScript-Templates
66
*
77
* Copyright 2011, Sebastian Tschan
@@ -17,13 +17,13 @@
1717
<meta charset="utf-8">
1818
<title>JavaScript Templates Test</title>
1919
<meta name="viewport" content="width=device-width">
20-
<link rel="stylesheet" href="http://blueimp.github.com/cdn/css/mocha.min.css">
20+
<link rel="stylesheet" href="http://blueimp.github.io/cdn/css/mocha.min.css">
2121
</head>
2222
<body>
2323
<div id="mocha"></div>
2424
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
25-
<script src="http://blueimp.github.com/cdn/js/mocha.min.js"></script>
26-
<script src="http://blueimp.github.com/cdn/js/expect.min.js"></script>
25+
<script src="http://blueimp.github.io/cdn/js/mocha.min.js"></script>
26+
<script src="http://blueimp.github.io/cdn/js/expect.min.js"></script>
2727
<script>
2828
mocha.setup('bdd');
2929
$(function () {

test/template.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/test.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates Test 2.1.0
2+
* JavaScript Templates Test 2.2.0
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -11,14 +11,16 @@
1111

1212
/*global beforeEach, afterEach, describe, it, expect, require */
1313

14-
(function (expect, tmpl) {
14+
(function (context, expect, tmpl) {
1515
'use strict';
1616

17-
if (typeof require !== 'undefined') {
17+
if (context.require === undefined) {
1818
// Override the template loading method:
1919
tmpl.load = function (id) {
20-
return require('fs')
21-
.readFileSync('./test/' + id + '.html', 'utf8');
20+
switch (id) {
21+
case 'template':
22+
return '{%=o.value%}';
23+
}
2224
};
2325
}
2426

@@ -130,7 +132,7 @@
130132
);
131133
});
132134

133-
it('Print empty string for escaped falsy values', function () {
135+
it('Print out escaped falsy values', function () {
134136
expect(
135137
tmpl(
136138
'{%=o.undefinedValue%}{% print(o.undefinedValue); %}' +
@@ -140,11 +142,11 @@
140142
data
141143
)
142144
).to.be(
143-
''
145+
'undefinedundefinednullnullfalsefalse00'
144146
);
145147
});
146148

147-
it('Print empty string for unescaped falsy values', function () {
149+
it('Print out unescaped falsy values', function () {
148150
expect(
149151
tmpl(
150152
'{%#o.undefinedValue%}{% print(o.undefinedValue, true); %}' +
@@ -154,7 +156,7 @@
154156
data
155157
)
156158
).to.be(
157-
''
159+
'undefinedundefinednullnullfalsefalse00'
158160
);
159161
});
160162

@@ -251,6 +253,7 @@
251253
});
252254

253255
}(
256+
this,
254257
this.expect || require('expect.js'),
255258
this.tmpl || require('../tmpl').tmpl
256259
));

tmpl.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript Templates 2.1.0
2+
* JavaScript Templates 2.2.0
33
* https://github.com/blueimp/JavaScript-Templates
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -48,7 +48,7 @@
4848
if (p2 === "=") {
4949
return "'+_e(" + p3 + ")+'";
5050
}
51-
return "'+(" + p3 + "||'')+'";
51+
return "'+" + p3 + "+'";
5252
}
5353
if (p4) { // evaluation start tag: {%
5454
return "';";
@@ -66,7 +66,7 @@
6666
"'" : "&#39;"
6767
};
6868
tmpl.encode = function (s) {
69-
return String(s || "").replace(
69+
return String(s).replace(
7070
tmpl.encReg,
7171
function (c) {
7272
return tmpl.encMap[c] || "";

tmpl.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)