paularmstrong/swig — Issue #54: Error in pre-compiled template causes template to not render in browser
Author: kamranayub
Created: 2012-02-17 04:13:16+00:00
State: open

I don't know the codebase well enough to pinpoint this line of code but here's my scenario to reproduce:

**Browser**

1. Have swig compile a valid template.

`var tmpl = swig.compile("{{ foo.foo }}");`

2. Call template with bad data:

`$("body").html(tmpl({ foo: 1 }));`

3. Nothing appears on screen

Stepping through the `tmpl` function, you will see Swig catch an error, throw a `new TemplateError(e)` but this simply returns an object with a `render` function that never gets executed.

In my compiled `swig.js` for browser, I changed line 193 (v0.9.4) from:

return new TemplateError(e);

to:

return new TemplateError(e).render();

This fixes the issue for me. I am not sure if this is the correct fix, however.

The docs for the browser seem to be a bit off. If I inspect `tmpl` in Chrome tools, I see:

```
function (source, options) {
return tmpl.render(source, options);
}
```

Which means to invoke it I use `tmpl(data)` not `tmpl.render(data)`.