sstephenson / eco

Embedded CoffeeScript templates
MIT License
1.71k stars 70 forks source link

eco pollutes context objects #57

Open osuushi opened 12 years ago

osuushi commented 12 years ago

In order to support the @safe and @escape helpers, eco adds these to the context object, which can make for confusing bugs.

Here's a very simplified version of where I ran into the problem:


sales = ({price: i, shipping:i, tax:i} for i in [0..10])

tpl = eco.compile "Price: <%= @price %>; Shipping: <%= @shipping %>; Tax: <%= @tax %><br>"

totals =
    price: 0
    shipping: 0
    tax: 0

for s in sales
    console.log tpl s
    totals[k] += v for k,v of s 

console.log tpl totals

The problem is calling tpl pollutes s with the escape property, which then gets concatenated repeatedly onto totals.escape. As a result, we end up with a garbled function string that begins "undefinedfunction (value)...". Then when we call tpl totals, this eco tries to call that garbled string as a function, and we get an error which was rather confusing to pin down.

For now, I'm working around this by shallow copying my inputs. But this is really not how a template library should be treating its inputs. Any changes made on the context object should be reversed before returning.