Skip to content

Commit

Permalink
enable custom filters
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunlee committed May 1, 2011
1 parent 86b112a commit 82c68a9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.swp
.git/*

test/*
11 changes: 10 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ via npm:
// Filters
jst.render('Hello {{ it.name|e }}', {name: '<strong>jst</strong>'});
jst.render('{{ it.entry|e|linebreaks }}', {entry: '...'});
jst.render('{{ it.value|add(100) }}', {value: 100});
jst.render('{{ it.value|add(123) }}', {value: 123});

// Custom filters
jst.addFilter('filterName', function(src) { ... });
jst.addFilters({anotherFilter: function(src) { ... }});
jst.render('{{ it.value|filterName }}', {value: 123});
jst.render('{{ it.value|anotherFilter }}', {value: 123});
// or
jst.addFilter('filterName', function(arg1, arg2, arg3) { return function(src) { ... }});
jst.render('{{ it.value|filterName(1, 2, 3) }}', {value: 123});

// Client side
<script src="jst.js"></script>
Expand Down
22 changes: 15 additions & 7 deletions lib/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,31 @@ exports.convert = function(src) {
});
}

exports.e = exports.escape = function(src) {
function escape(src) {
return typeof src !== 'string' ? src : src.replace(htmlre, htmlEscape);
}

exports.linebreaks = function(src) {
function linebreaks(src) {
return '<p>' + src.split(/\r\n|\n/g).join('</p><p>') + '</p>';
}

exports.linebreaksbr = function(src) {
function linebreaksbr(src) {
return src.replace(linere, '<br>$1');
}

exports.md = exports.markdown = function(src) {
return src; // TODO:
function add(value) {
return function(src) { return Number(value) + Number(src); };
}

exports.add = function(value) {
return function(src) { return Number(value) + Number(src); };
function markdown(src) {
return src; // TODO:
}

exports.filters = {
escape: escape, e: escape,
linebreaks: linebreaks,
linebreaksbr: linebreaksbr,
markdown: markdown, md: markdown,
add: add
};

2 changes: 1 addition & 1 deletion lib/i18n.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// i18n

// _(ctx)
exports.gettext = function(ctx) {
exports.gettext = function(ctx, args) {
return ctx; // TODO:
}

13 changes: 11 additions & 2 deletions lib/jst.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var fs = require('fs'),
filters = require('./filters'),
i18n = require('./i18n');

exports.version = '0.0.8';
exports.version = '0.0.9';

var _cache = {},
_files = {},
Expand All @@ -26,6 +26,15 @@ exports.configure = function(options) {
_options[prop] = options[prop];
}

exports.addFilter = function(name, fn) {
filters.filters[name] = fn;
}

exports.addFilters = function(newFilters) {
for (var name in newFilters)
filters.filters[name] = newFilters[name];
}

// compiler

const prefixes = {
Expand Down Expand Up @@ -81,7 +90,7 @@ var compile = exports.compile = function(ctx) {
var fn = new Function('it, _, filters', code);

return function(args) {
return fn.call(this, args, i18n.gettext, filters);
return fn.call(this, args, i18n.gettext, filters.filters);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jst",
"description": "Node JavaScript Template, A pretty high performance template engine",
"version": "0.0.8",
"version": "0.0.9",
"author": "Shaun Li <shonhen@gmail.com>",
"keywords": ["template", "engine", "jst"],
"main": "./lib/jst.js"
Expand Down

0 comments on commit 82c68a9

Please sign in to comment.