Skip to content

Commit

Permalink
removing options from parameters in template.substitute()
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Apr 9, 2012
1 parent 136dbed commit 95115bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 41 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Template
Template is fast and context aware template engine with conditional replacement, iterations and filters.

- [Demo](http://jsfiddle.net/tbela99/ygWKc/1/)
- [Compare to Mustache.js and Hogan.js](http://jsperf.com/template-mustachejs-hogan)
- Compare to Mustache.js and Hogan.js [here](http://jsperf.com/template-mustachejs-hogan) and [here](http://jsperf.com/template-mustachejs-hogan/2)

How to use
---------------------
Expand Down Expand Up @@ -242,7 +242,7 @@ substitute the given object into the given string template.

var template = new Template();

var output = template.substitute(string, data[, options]);
var output = template.substitute(string, data);

### Returns:

Expand All @@ -252,7 +252,6 @@ substitute the given object into the given string template.

- string - (*string*) input template
- data - (*object*) global context
- options - (*object*, optional) override some of the template instance options.


Template Method: compile
Expand Down
25 changes: 14 additions & 11 deletions Source/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ provides: [Template]
this.options = Object.append({}, options);
this.hash = Object.values(this.options)
},
setOptions: function (options) {

if(options) Object.append(this.options, options);
this.hash = Object.values(this.options) + Object.values(this.filters) + Object.values(this.modifiers);

return this
},
addFilter: function (name, fn) {

if(typeof name == 'object') Object.each(name, function (value, name) { this.filters[name] = value }, this);

else this.filters[name] = fn;

this.hash = Object.values(this.options) + Object.values(this.filters) + Object.values(this.modifiers);
return this
return this.setOptions()
},
addModifier: function (name, fn) {

Expand All @@ -69,26 +75,23 @@ provides: [Template]
};

this.hash = Object.values(this.options) + Object.values(this.filters) + Object.values(this.modifiers);
return this
return this.setOptions()
},
html: function (template, data, options) { return Elements.from(this.substitute(template, data, options)) },
compile: function (template, options) {

if(options != undef || !this.cache[template + this.hash]) {

Object.append(this.options, options);
this.hash = Object.values(this.options) + Object.values(this.filters) + Object.values(this.modifiers) + Object.values(options || {});
options = Object.append({}, this.options, {filters: this.filters, modifiers: this.modifiers});
}
if(options && options != this.options) this.setOptions(options);

options = Object.append({}, this.options, {filters: this.filters, modifiers: this.modifiers});
if(this.cache[template + this.hash]) return this.cache[template + this.hash];

this.cache[template + this.hash] = compile(template, options || {}, this.hash);
return this.cache[template + this.hash];
},
substitute: function (template, data, options) {
substitute: function (template, data) {

return this.compile(template, options)(data)
if(!this.cache[template + this.hash]) this.compile(template, this.options);
return this.cache[template + this.hash](data)
}
};

Expand Down
39 changes: 12 additions & 27 deletions Test/specs/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,30 +284,29 @@ describe("Template test", function() {

it("Custom tags {{title}} spends {{calc}}", function() {

expect(new Template().substitute('{{title}} spends {{calc}}', {
expect(new Template({
begin: '{{',
end: '}}'
}).substitute('{{title}} spends {{calc}}', {
title: "Joe",
calc: function() {
return 2 + 4;
}
},
{
begin: '{{',
end: '}}'
})).toEqual("Joe spends 6");
});

it("Custom tags [[title]] spends [[calc]]", function() {

expect(new Template().substitute('[[title]] spends [[calc]]', {
expect(new Template({
begin: '[[',
end: ']]'
}).substitute('[[title]] spends [[calc]]', {
title: "Joe",
calc: function() {
return 2 + 4;
}
},
{
begin: '[[',
end: ']]'
})).toEqual("Joe spends 6");
}
)).toEqual("Joe spends 6");
})
});

Expand All @@ -325,20 +324,6 @@ describe("Template test", function() {

it("using custom parse {lambda:nil}some text{/lambda:nil}", function() {

expect(new Template().substitute('{lambda:nil}some text{/lambda:nil}', {
title: "Joe",
calc: function() {
return 2 + 4;
}
},
{

parse: function () { return 'Yes!' }
})).toEqual("Yes!")
});

it("using custom parse with new Template() {lambda:nil}some text{/lambda:nil}", function() {

expect(new Template({

parse: function () { return 'Yes!' }
Expand All @@ -347,9 +332,9 @@ describe("Template test", function() {
calc: function() {
return 2 + 4;
}
})).toEqual("Yes!")
}
)).toEqual("Yes!")
})
})

});

0 comments on commit 95115bc

Please sign in to comment.