From f790ef6bd9218d30ccfd230d8cfbc847d9a069c7 Mon Sep 17 00:00:00 2001 From: nybras Date: Tue, 11 Oct 2011 15:21:14 -0300 Subject: [PATCH] improving javascript template generation by splitting the template-renderer functionality from the template itself --- src/cli.coffee | 4 +++- src/coffeekup.coffee | 30 +++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/cli.coffee b/src/cli.coffee index f6bdcee..217a19b 100644 --- a/src/cli.coffee +++ b/src/cli.coffee @@ -49,7 +49,9 @@ usage = ''' ''' switches = [ - ['--js', 'compile template to js function'] + ['-j', '--js', 'compile template to js function (template + embedded renderer)'] + ['-b', '--bare', 'use with -j to compile template to js (template only)' ] + ['-c', '--core', 'use with -j to compile renderer to js (renderer only)' ] ['-n', '--namespace [name]', 'global object holding the templates (default: "templates")'] ['-w', '--watch', 'watch templates for changes, and recompile'] ['-o', '--output [dir]', 'set the directory for compiled html'] diff --git a/src/coffeekup.coffee b/src/coffeekup.coffee index 339bf89..a87b054 100644 --- a/src/coffeekup.coffee +++ b/src/coffeekup.coffee @@ -302,24 +302,28 @@ coffeekup.compile = (template, options = {}) -> for t in coffeekup.tags if template.indexOf(t) > -1 or hardcoded_locals.indexOf(t) > -1 tags_used.push t - + tag_functions += "var #{tags_used.join ','};" for t in tags_used tag_functions += "#{t} = function(){return __ck.tag('#{t}', arguments);};" - # Main function assembly. - code = tag_functions + hardcoded_locals + skeleton + # # Main function assembly. + code = "" - code += "__ck.doctypes = #{JSON.stringify coffeekup.doctypes};" - code += "__ck.coffeescript_helpers = #{JSON.stringify coffeescript_helpers};" - code += "__ck.self_closing = #{JSON.stringify coffeekup.self_closing};" - - # If `locals` is set, wrap the template inside a `with` block. This is the - # most flexible but slower approach to specifying local variables. - code += 'with(data.locals){' if options.locals - code += "(#{template}).call(data);" - code += '}' if options.locals - code += "return __ck.buffer.join('');" + # If bare is used, the main mechanism is stripped from template + unless options.bare && !options.core + code += tag_functions + hardcoded_locals + skeleton + code += "__ck.doctypes = #{JSON.stringify coffeekup.doctypes};" + code += "__ck.coffeescript_helpers = #{JSON.stringify coffeescript_helpers};" + code += "__ck.self_closing = #{JSON.stringify coffeekup.self_closing};" + + unless options.core && !options.bare + # If `locals` is set, wrap the template inside a `with` block. This is the + # most flexible but slower approach to specifying local variables. + code += 'with(data.locals){' if options.locals + code += "(#{template}).call(data);" + code += '}' if options.locals + code += "return __ck.buffer.join('');" new Function('data', code)