index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. var lib = require('./src/lib');
  3. var _require = require('./src/environment'),
  4. Environment = _require.Environment,
  5. Template = _require.Template;
  6. var Loader = require('./src/loader');
  7. var loaders = require('./src/loaders');
  8. var precompile = require('./src/precompile');
  9. var compiler = require('./src/compiler');
  10. var parser = require('./src/parser');
  11. var lexer = require('./src/lexer');
  12. var runtime = require('./src/runtime');
  13. var nodes = require('./src/nodes');
  14. var installJinjaCompat = require('./src/jinja-compat');
  15. // A single instance of an environment, since this is so commonly used
  16. var e;
  17. function configure(templatesPath, opts) {
  18. opts = opts || {};
  19. if (lib.isObject(templatesPath)) {
  20. opts = templatesPath;
  21. templatesPath = null;
  22. }
  23. var TemplateLoader;
  24. if (loaders.FileSystemLoader) {
  25. TemplateLoader = new loaders.FileSystemLoader(templatesPath, {
  26. watch: opts.watch,
  27. noCache: opts.noCache
  28. });
  29. } else if (loaders.WebLoader) {
  30. TemplateLoader = new loaders.WebLoader(templatesPath, {
  31. useCache: opts.web && opts.web.useCache,
  32. async: opts.web && opts.web.async
  33. });
  34. }
  35. e = new Environment(TemplateLoader, opts);
  36. if (opts && opts.express) {
  37. e.express(opts.express);
  38. }
  39. return e;
  40. }
  41. module.exports = {
  42. Environment: Environment,
  43. Template: Template,
  44. Loader: Loader,
  45. FileSystemLoader: loaders.FileSystemLoader,
  46. NodeResolveLoader: loaders.NodeResolveLoader,
  47. PrecompiledLoader: loaders.PrecompiledLoader,
  48. WebLoader: loaders.WebLoader,
  49. compiler: compiler,
  50. parser: parser,
  51. lexer: lexer,
  52. runtime: runtime,
  53. lib: lib,
  54. nodes: nodes,
  55. installJinjaCompat: installJinjaCompat,
  56. configure: configure,
  57. reset: function reset() {
  58. e = undefined;
  59. },
  60. compile: function compile(src, env, path, eagerCompile) {
  61. if (!e) {
  62. configure();
  63. }
  64. return new Template(src, env, path, eagerCompile);
  65. },
  66. render: function render(name, ctx, cb) {
  67. if (!e) {
  68. configure();
  69. }
  70. return e.render(name, ctx, cb);
  71. },
  72. renderString: function renderString(src, ctx, cb) {
  73. if (!e) {
  74. configure();
  75. }
  76. return e.renderString(src, ctx, cb);
  77. },
  78. precompile: precompile ? precompile.precompile : undefined,
  79. precompileString: precompile ? precompile.precompileString : undefined
  80. };