aboutsummaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/README.mkd41
-rw-r--r--js/luigi-template.js9
-rw-r--r--js/test/cache.js7
3 files changed, 35 insertions, 22 deletions
diff --git a/js/README.mkd b/js/README.mkd
index 9f3e5e6..63d50e9 100644
--- a/js/README.mkd
+++ b/js/README.mkd
@@ -63,17 +63,8 @@ The built-in templates are:
* `uc`: Upper-case string.
* `lc`: Lower-case string.
-* `s`: Returns `""` if the value is 1, and `"s"` otherwise. Used to
- pluralize values. Example:
-
- console.log(LuigiTemplate.run('a: a%{a|s}, b: b%{b|s}, c: c%{c|s}'', {
- a: 0,
- b: 1,
- c: 2,
- }));
-
- // prints "a: as, b: b, c: cs"
-
+* `s`: Pluralize a value by returning `""` if the value is 1, and
+ `"s"` otherwise.
* `length`: Get the length of an array.
* `trim`: Trim leading and trailing whitespace from a string.
* `h`: HTML-escape a string value.
@@ -100,8 +91,8 @@ of global filters (`LuigiTemplate.FILTERS`), like so:
// prints "hello bar-<Paul>-bar"
-You can also create a custom filters and limit it to a particular
-template by passing a hash as the second parameter to the
+You can also create a custom filter and limit it to a particular
+template by passing a custom filter hash as the second parameter to the
`LuigiTemplate` constructor, like this:
// create template with custom template-specific filter
@@ -118,8 +109,7 @@ template by passing a hash as the second parameter to the
// prints "hello bar-<Paul>-bar"
-You can pass arguments to your custom filters as well. Here's an
-example:
+You can pass arguments to your custom filters. Here's an example:
// create template with custom template-specific filter named
// "wrap", which wraps the value in the given filter parameters
@@ -167,6 +157,27 @@ Here's how you create a template cache:
// prints "hello <PAUL%gt;"
+Finally, template caches use their own set of custom filters by passing
+the custom custom when creating a template cache:
+
+ // create template cache with a custom filter named "reverse"
+ var cache = LuigiTemplate.cache({
+ hello: 'hello %{name | uc | reverse | h}'
+ }, {
+ reverse: function(s) {
+ var cs = (s || '').split('');
+ cs.reverse();
+ return cs.join('');
+ },
+ });
+
+ // run template, print result to console
+ console.log(cache.run('hello', {
+ name: '<Paul>',
+ }));
+
+ // prints "hello %gt;LUAP&lt;"
+
Documentation
-------------
*TODO*
diff --git a/js/luigi-template.js b/js/luigi-template.js
index 473938f..f01d976 100644
--- a/js/luigi-template.js
+++ b/js/luigi-template.js
@@ -288,8 +288,9 @@ LuigiTemplate = (function() {
T.prototype.run = run;
// declare cache constructor
- T.Cache = function(templates, try_dom) {
+ T.Cache = function(templates, filters, try_dom) {
this.templates = templates;
+ this.filters = filters || FILTERS;
this.try_dom = !!try_dom;
this.cache = {};
};
@@ -310,15 +311,15 @@ LuigiTemplate = (function() {
}
// cache template
- this.cache[key] = new T(s);
+ this.cache[key] = new T(s, this.filters);
}
// run template
return this.cache[key].run(args);
};
- T.cache = function(args) {
- return new T.Cache(args);
+ T.cache = function(args, filters) {
+ return new T.Cache(args, filters || FILTERS);
}
// declare domcache constructor
diff --git a/js/test/cache.js b/js/test/cache.js
index 0b68b3d..22490c1 100644
--- a/js/test/cache.js
+++ b/js/test/cache.js
@@ -40,14 +40,15 @@
it('cache with custom filters', function() {
- var cache = new LuigiTemplate.Cache({
- foo: ['foo%{bar | barify}'],
+ var cache = LuigiTemplate.cache({
+ foo: ['foo%{bar | cache-barify}'],
}, {
- barify: function(s) {
+ 'cache-barify': function(s) {
return 'bar-' + s + '-bar';
},
});
+ // run template from cache, get result
var r = cache.run('foo', {
bar: 'foo',
});