Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improving javascript template generation by splitting the template-re…

…nderer functionality from the template itself
  • Loading branch information...
commit f790ef6bd9218d30ccfd230d8cfbc847d9a069c7 1 parent e8295b4
@arboleya arboleya authored
Showing with 20 additions and 14 deletions.
  1. +3 −1 src/cli.coffee
  2. +17 −13 src/coffeekup.coffee
View
4 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']
View
30 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)
Please sign in to comment.
Something went wrong with that request. Please try again.