engines.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var _ = require('lodash');
  2. // TODO(mklabs):
  3. // - handle cache
  4. // - implement adpaters for others engines (but do not add hard deps on them,
  5. // should require manual install for anything that is not an underscore
  6. // template)
  7. // - put in multiple files, possibly other packages.
  8. // engines
  9. var engines = module.exports;
  10. // Underscore
  11. // ----------
  12. // Underscore templates facade.
  13. //
  14. // Special kind of markers `<%%` for opening tags can be used to escape the
  15. // placeholder opening tag. This is often useful for templates including
  16. // snippet of templates you don't want to be interpolated.
  17. var matcher = /<%%([^%]+)%>/g;
  18. var detecter = /<%%?[^%]+%>/;
  19. engines.underscore = function underscore(source, data) {
  20. source = source.replace(matcher, function (m, content) {
  21. // let's add some funny markers to replace back when templating is done,
  22. // should be fancy enough to reduce frictions with files using markers like
  23. // this already.
  24. return '(;>%%<;)' + content + '(;>%<;)';
  25. });
  26. source = _.template(source)(data);
  27. source = source
  28. .replace(/\(;>%%<;\)/g, '<%')
  29. .replace(/\(;>%<;\)/g, '%>');
  30. return source;
  31. };
  32. engines.underscore.detect = function detect(body) {
  33. return detecter.test(body);
  34. };