File: hamlc.coffee
Defined in: | node_modules/grunt-codo/node_modules/codo/node_modules/haml-coffee/src |
Variables Summary
- __expressCache =
-
{}
Express 3 template Cache
- module.exports =
-
{ /* Render the Haml Coffee template into static HTML. @see {Compiler} for a complete list of the supported compiler options. @param [String] source the Haml Coffee source @param [Object] context context for the template @param [Object] options the compiler options @return [Function] the template */ render: function(source, context, options) { var compiler, template; if (context == null) { context = {}; } if (options == null) { options = {}; } /* Ensure placement is set to standalone for static rendering. */ options.placement = 'standalone'; compiler = new Compiler(options); compiler.parse(source); template = new Function(CoffeeScript.compile(compiler.precompile(), { bare: true })); return template.call(context); }, /* Compile the Haml Coffee template into a JavaScript function. @see {Compiler} for a complete list of the supported compiler options. @param [String] source the Haml Coffee source @param [Object] options the compiler options @return [Function] the template */ compile: function(source, options) { var compiler, template; if (options == null) { options = {}; } compiler = new Compiler(options); compiler.parse(source); template = new Function(CoffeeScript.compile(compiler.precompile(), { bare: true })); return function(params) { return template.call(params); }; }, /* Creates the JavaScript Template. @see {Compiler} for a complete list of the supported compiler options. @param [String] source the Haml Coffee source @param [String] name the template name @param [String] namespace the template namespace @param [Object] options the compiler options @return [String] the template source code */ template: function(source, name, namespace, options) { var compiler; if (options == null) { options = {}; } /* Extend the options with the name and namespace so that the compiler has these configuration properties from the beginning and that the API for this method can stay the same. */ options.namespace = namespace; options.name = name; compiler = new Compiler(options); compiler.parse(source); return CoffeeScript.compile(compiler.render()); }, /* Express 3 templating interface with template function cache. When the template function cache is enabled by setting `cache` in the options to true, the compiled JavaScript template function is cached, which improves speed a lot, since it it only parses, generates and compiles to template once. @overload __express(filename, callback) Compiles and renders a template @param [String] filename the template file path @param [Function] the callback @overload __express(filename, options, callback) Compiles and renders a template @param [String] filename the template file path @param [Object] options the compiler options and template locals @option options [Boolean] cache whether to cache the template or not @param [Function] the callback */ __express: function(filename, options, callback) { var err, source; if (!!(options && options.constructor && options.call && options.apply)) { callback = options; options = {}; } try { if (options.cache && __expressCache[filename]) { return callback(null, __expressCache[filename](options)); } else { options.filename = filename; source = fs.readFileSync(filename, 'utf8'); if (options.cache) { __expressCache[filename] = module.exports.compile(source, options); return callback(null, __expressCache[filename](options)); } else { return callback(null, module.exports.compile(source, options)(options)); } } } catch (_error) { err = _error; return callback(err); } } }
Facade to Haml Coffee for easy template function compiling and JST template rendering.