Permalink
Browse files

Merge pull request #7 from paularmstrong/23ea88f5

Added 2 Filters and Documentation
  • Loading branch information...
2 parents 9fb0bf7 + 23ea88f commit 75a53d3b68ca767e2df3cb6e635b34a846c349a1 @skid committed Aug 16, 2011
Showing with 105 additions and 9 deletions.
  1. +2 −0 .npmignore
  2. +73 −9 README.md
  3. +11 −0 lib/filters.js
  4. +19 −0 tests/filters.test.js
View
@@ -0,0 +1,2 @@
+.DS_Store
+tests/*
View
@@ -5,7 +5,7 @@ A fast django-like templating engine for node.js.
Node-T is a templating engine inspired by the django syntax. It has a few extensions and the templates are compiled to native javascript functions which make them really fast. For now it's synchronous, but once a template is read and compiled, it is cached in memory.
### Example template code
-
+
<html>
<body>
<h1>Example</h1>
@@ -62,6 +62,70 @@ Used to print a variable to the template. If the variable is not in the context
<p>First Name: {{users.0.first_name}}</p>
+#### Variable Filters
+
+Used to modify variables. Filters are added directly after variable names, separated by the pipe (|) character. You can chain multiple filters together, applying one after the other in succession.
+
+ {{ foo|reverse|join(' ')|title }}
+
+##### default(default_value)
+
+If the variable is `undefined`, `null`, or `false`, a default return value can be specified.
+
+ {{ foo|default('foo is not defined') }}
+
+##### lower
+
+Return the variable in all lowercase letters.
+
+##### upper
+
+Return the variable in all uppercase letters
+
+##### capitalize
+
+Capitalize the first character in the string.
+
+##### title
+
+Change the output to title case–the first letter of every word will uppercase, while all the rest will be lowercase.
+
+##### join
+
+If the value is an Array, you can join each value with a delimiter and return it as a string.
+
+ {{ authors|join(', ') }}
+
+##### reverse
+
+If the value is an Array, this filter will reverse all items in the array.
+
+##### length
+
+Return the `length` property of the value.
+
+##### url_encode
+
+Encode a URI component.
+
+##### url_decode
+
+Decode a URI component.
+
+##### json_encode
+
+Return a JSON string of the variable.
+
+##### striptags
+
+Strip all HTML/XML tags.
+
+##### date
+
+Convert a valid date into a format as specified. Mostly conforms to (php.net's date formatting)[http://php.net/date].
+
+ {{ post.created|date('F jS, Y') }}
+
### Comment tags
Comment tags are simply ignored. Comments can't span multitple lines.
@@ -77,7 +141,7 @@ Check django's template inheritance system for more info. Unlike django, the blo
#### include
Includes a template in it's place. The template is rendered within the current context. Does not requre closing with {% end %}.
-
+
{% include template_path %}
{% include "path/to/template.js" %}
@@ -88,7 +152,7 @@ You can iterate arrays and objects. Access the current iteration index through '
{% for x in y %}
<p>{% forloop.index %}</p>
{% end %}
-
+
#### if
Supports the following expressions. No else tag yet.
@@ -108,7 +172,7 @@ Supports the following expressions. No else tag yet.
Use slots where you want highly customized content that depends heavily on dynamic data. They work in pair with widget functions that you can write yourself.
Template code
-
+
<div>
{% slot main %}
</div>
@@ -121,14 +185,14 @@ Node.js code
context.slots = {
main: [
"<h1>This is a paragraph as a normal string.</h1>", // String
-
+
{ tagname: 'analytics', // Widget object
uaCode: 'UA-XXXXX-X' },
],
-
+
sidebar: [
"<h3>Navigation</h3>", // String
-
+
{ tagname: 'navigation', // Widget object
links: [
'/home',
@@ -137,7 +201,7 @@ Node.js code
]}
]
}
-
+
context.widgets = {
analytics: function (context) {
// this inside widget functions is bound to the widget object
@@ -150,7 +214,7 @@ Node.js code
return html;
}
}
-
+
template.render(context)
Result
View
@@ -113,6 +113,9 @@ _dateFormats = {
}
};
+exports.default = function (input, def) {
+ return (input || typeof input === 'number') ? input : def;
+};
exports.lower = function (input) {
return input.toString().toLowerCase();
@@ -140,6 +143,14 @@ exports.join = function (input, separator) {
}
};
+exports.reverse = function (input) {
+ if (Array.isArray(input)) {
+ return input.reverse();
+ } else {
+ return input;
+ }
+};
+
exports.length = function (input) {
return input.length;
};
View
@@ -1,5 +1,17 @@
var filters = require('../lib/filters');
+exports.default = function (test) {
+ var defOut = 'blah';
+ test.strictEqual('foo', filters.default('foo', defOut), 'string not overridden by default');
+ test.strictEqual(0, filters.default(0, defOut), 'zero not overridden by default');
+
+ test.strictEqual(defOut, filters.default('', defOut), 'empty string overridden by default');
+ test.strictEqual(defOut, filters.default(undefined, defOut), 'default overrides undefined');
+ test.strictEqual(defOut, filters.default(null, defOut), 'default overrides null');
+ test.strictEqual(defOut, filters.default(false, defOut), 'default overrides false');
+ test.done();
+};
+
exports.lower = function (test) {
var input = 'BaR';
test.strictEqual('bar', filters.lower(input));
@@ -39,6 +51,13 @@ exports.join = function (test) {
test.done();
};
+exports.reverse = function (test) {
+ test.deepEqual([3, 2, 1], filters.reverse([1, 2, 3]), 'reverse array');
+ test.strictEqual('asdf', filters.reverse('asdf'), 'reverse string does nothing');
+ test.deepEqual({ 'foo': 'bar' }, filters.reverse({ 'foo': 'bar' }), 'reverse object does nothing');
+ test.done();
+};
+
exports.length = function (test) {
var input = [1, 2, 3];
test.strictEqual(3, filters.length(input));

0 comments on commit 75a53d3

Please sign in to comment.