Skip to content

Commit 7e8cb32

Browse files
committedJul 14, 2015
Adding interpolation rule for conditional statements
1 parent 0245e52 commit 7e8cb32

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed
 

‎src/interpolation/Interpolator.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Interpolator(parser) {
2121
this.interpolateString = function(context, value) {
2222
return parser.assemble(context, parser.parse(value))
2323
},
24-
24+
2525

2626
this.interpolate = function(context, value, interpolationPolicy) {
2727
var index = 0
@@ -33,14 +33,12 @@ function Interpolator(parser) {
3333
return value;
3434
}
3535
var interpolationRules = self.rules[Object.prototype.toString.call(value)]
36-
if (!interpolationRules)
36+
if (!interpolationRules)
3737
return value
3838

3939
while(index < interpolationRules.length && !( matched = interpolationRules[index].match(value) )) index++
40-
41-
if (!matched)
40+
if (!matched)
4241
return value
43-
4442
return interpolationRules[index].action(self, context, value)
4543
}
4644
}
@@ -50,36 +48,45 @@ Interpolator.prototype.rules = {
5048
{
5149
name: "context reference",
5250
match: function(value) {("@" === value) },
53-
action: function(interpolator, context, value) { return context }
51+
action: function(interpolator, context, value) { return context }
5452
},
5553
{
5654
name: "eval shorthand",
5755
match: function(value) { return value[0] == '#' },
58-
action: function(interpolator, context, value) {
56+
action: function(interpolator, context, value) {
5957
with(context) {
6058
return eval(value.slice(1))
61-
}
59+
}
6260
}
6361
},
6462
{
6563
name: "context member reference",
6664
match: function(value) { return value[0] == '@' },
67-
action: function(interpolator, context, value) {
68-
return interpolator.readContextPath(context, value.slice(1))
65+
action: function(interpolator, context, value) {
66+
return interpolator.readContextPath(context, value.slice(1))
67+
}
68+
},
69+
{
70+
name: "conditional statement",
71+
match: function(value) { return /(.+)(>|==|===|!=|!==|<|>=|<=)(.+)/.test(value) },
72+
action: function(interpolator, context, value) {
73+
with(context) {
74+
return eval(value)
75+
}
6976
}
7077
},
7178
{
7279
name: "markup resolver",
7380
match: function(value) { return value.indexOf("[") > -1 },
74-
action: function(interpolator, context, value) {
75-
return interpolator.interpolateString(context, value)
81+
action: function(interpolator, context, value) {
82+
return interpolator.interpolateString(context, value)
7683
}
7784
}],
7885
"[object Array]": [
7986
{
8087
name: "object field interpolator",
8188
match: function(value) { return true },
82-
action: function(interpolator, context, value) {
89+
action: function(interpolator, context, value) {
8390
var result = []
8491
for (var index = 0; index < value.length; index++) {
8592
result.push(interpolator.interpolate(context, value[index]))
@@ -96,7 +103,7 @@ Interpolator.prototype.rules = {
96103
{
97104
name: "legacy {template:'...'} field resolver",
98105
match: function(value) { return "template" in value },
99-
action: function(interpolator, context, value) {
106+
action: function(interpolator, context, value) {
100107
console.warn("obsoleted functionality: { template: value }, use [hbs]..[/hbs] instead")
101108
var template = value.template
102109
var compiled = handlebars.compile(template)
@@ -106,7 +113,7 @@ Interpolator.prototype.rules = {
106113
{
107114
name: "object field interpolator",
108115
match: function(value) { return true },
109-
action: function(interpolator, context, value) {
116+
action: function(interpolator, context, value) {
110117
var result = {}
111118
var keys = Object.keys(value)
112119
for (var index = 0; index < keys.length; index++) {

0 commit comments

Comments
 (0)
Please sign in to comment.