Skip to content

Commit

Permalink
Swig for Ender
Browse files Browse the repository at this point in the history
  • Loading branch information
naholyr committed Dec 30, 2011
0 parents commit eb30198
Show file tree
Hide file tree
Showing 6 changed files with 1,611 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
swig
index.js.bak
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
swig
index.js.bak
22 changes: 22 additions & 0 deletions ender.js
@@ -0,0 +1,22 @@
!function ($) {
var _cache={},
_compile = function(id, filename) {
if (!_cache[id]) {
var e = document.getElementById(id),
s = e ? e.innerHTML : id,
m = s.match(/\{%\s*extends\s+['"]?(.*?)['"]?\s*%\}/),
f = e ? id : filename;
if (!f) throw new Error("Filename is required when rendering direct source");
if (m) _compile(m[1]);
_cache[id] = swig.compile(e ? e.innerHTML : id, {filename: f});

This comment has been minimized.

Copy link
@paularmstrong

paularmstrong Jan 2, 2012

FYI: Swig's compile step does its own internal caching, so this seems unnecessary.

This comment has been minimized.

Copy link
@naholyr

naholyr Jan 2, 2012

Author Owner

Yes, this is only because I wanted to cache the additional work I do here (parsing for extends blocks and compiling corresponding templates).
By the way, it should be done for include too.

Not sure the cache here is required anyway, it's just a matter of "executing regular expression" versus "another hash in memory".

}
return _cache[id];
},
_render = function (id, data, filename) {
return _compile(id, filename)(data);
};
$.ender({
swig: swig,
render: _render
});
}(ender);

0 comments on commit eb30198

Please sign in to comment.