documentcloud/underscore — Issue #400: New template refactor doesn't allow compilation
Author: sylvinus
Created: 2011-12-08 15:12:47+00:00
State: open

Hi,

We've been using the _.template function on the server-side to "pre-compile" templates before sending them to the clients inside our main JS file.

Basically, we had a compile function like this :

```javascript
compileToStaticFunction: function(str) {
return _.template(str).toString();
}
```

It was very helpful to bundle our html templates directly as javascript functions before a closure compiler pass.

Unfortunately this doesn't work anymore with underscore 1.2.x because of the wrapping at the end of _.template :

```
return function(data) {
return func.call(this, data, _);
};
```

Now _.template(str).toString() returns this function body which is useless.

I don't care having to patch our implementation but it doesn't seem to be possible now without copy-pasting the _.template function directly inside our code. What would be nice is to replace the snippet above by:

```
var ret = function(data) {
return func.call(this, data, _);
};
ret.func = func;
return ret;
```

So that we could get the compiled function body by doing ```_.template(str).func.toString();```. What do you think?

Thanks in advance!