Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multifile compilation support and tinier output with --js option #100

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ On the browser:
$('body').append(templates.template({foo: 'bar'}));
</script>

This is one of many browser deployment possibilities, pre-compiling your template on the server to a standalone function. To see all serving suggestions, check out [regular](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/regular/index.html), [decaf](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/decaf/index.html) and [crème](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/creme/index.html).
This is one of many browser deployment possibilities, pre-compiling your template on the server to a standalone function. To see all serving suggestions, check out [regular](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/regular/index.html), [decaf](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/decaf/index.html), [crème](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/creme/index.html), and [sugar](http://github.com/mauricemach/coffeekup/blob/master/examples/browser/sugar/index.html).

Command-line:

$ coffeekup -h

Usage:
coffeekup [options] path/to/template.coffee
coffeekup [options] [path/to/template.coffee OR path]

--js compile template to js function
-n, --namespace global object holding the templates (default: "templates")
Expand All @@ -205,9 +205,34 @@ Command-line:
-p, --print print the compiled html to stdout
-f, --format apply line breaks and indentation to html output
-u, --utils add helper locals (currently only "render")
-i, --include add hardcoded helpers to each template from file contents
-v, --version display CoffeeKup version
-h, --help display this help message

CLI usage:
// compile all templates in a directory into single file called 'templates.js'
coffeekup --js ./templates

// compile all templates in a directory into single file called 'My.Templates.js'
coffeekup --js -n 'My.Templates' ./templates

// include files from directory containing helpers
coffeekup --js -i ./helpers ./templates

Namespaced Templates are supported by convention from the directory structure:

Given dir structure:
- templates
MyTemplate.coffee
- moduleSpecificTemplates
MyIndex.coffee
- deepTemplates
abc.coffee
Yields:
this.templates["MyTemplate"] = ...
this.templates["moduleSpecificTemplates"]["MyIndex"] = ...
this.templates["moduleSpecificTemplates"]["deepTemplates"]["abc"] = ...

See [/examples](http://github.com/mauricemach/coffeekup/tree/master/examples) for complete versions (you have to run `cake build` first).

Please note that even though all examples are given in CoffeeScript, you can also use their plain JavaScript counterparts just fine.
Expand Down
22 changes: 19 additions & 3 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,23 @@ When installing CoffeeKup with `npm install coffeekup -g`, you get a `coffeekup`

$ coffeekup -h

Usage:
coffeekup [options] path/to/template.coffee
When you provide a directory instead of a file for the template, it will apply namespacing to the templates
according to the folder structure. So given directory structure:
- templates
MyTemplate.coffee
- moduleSpecificTemplates
MyIndex.coffee
- deepTemplates
abc.coffee
Yields:
this.templates["MyTemplate"] = ...
this.templates["moduleSpecificTemplates"]["MyIndex"] = ...
this.templates["moduleSpecificTemplates"]["deepTemplates"]["abc"] = ...

###Usage:
`coffeekup [options] path/to/template.coffee`
_or_
`coffeekup [options] path/to/templates`

--js compile template to js function
-n, --namespace global object holding the templates (default: "templates")
Expand All @@ -240,5 +255,6 @@ When installing CoffeeKup with `npm install coffeekup -g`, you get a `coffeekup`
-p, --print print the compiled html to stdout
-f, --format apply line breaks and indentation to html output
-u, --utils add helper locals (currently only "render")
-i, --include add hardcoded helpers to each template from file contents
-v, --version display CoffeeKup version
-h, --help display this help message
-h, --help display this help message
2 changes: 1 addition & 1 deletion examples/browser/decaf/build
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env sh
coffeekup --js template.coffee
../../../bin/coffeekup --js template.coffee

53 changes: 36 additions & 17 deletions examples/browser/decaf/template.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
(function(){
this.templates || (this.templates = {});
this.templates["template"] = function anonymous(data) {
var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = function(){return __ck.tag('i', arguments);};li = function(){return __ck.tag('li', arguments);};p = function(){return __ck.tag('p', arguments);};s = function(){return __ck.tag('s', arguments);};th = function(){return __ck.tag('th', arguments);};u = function(){return __ck.tag('u', arguments);};ul = function(){return __ck.tag('ul', arguments);};var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; };
var coffeescript, comment, doctype, h, ie, tag, text, __ck, _ref, _ref2;
this.templates || (this.templates = {});
var createBuilder = function anonymous(data) {
var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; };
var coffeescript, comment, doctype, h, ie, tag, text, yield, __ck, _ref, _ref2;
if (data == null) {
data = {};
}
if ((_ref = data.format) != null) {
_ref;
} else {
if ((_ref = data.format) == null) {
data.format = false;
};
if ((_ref2 = data.autoescape) != null) {
_ref2;
} else {
}
if ((_ref2 = data.autoescape) == null) {
data.autoescape = false;
};
}
__ck = {
buffer: [],
compile: function() {
return this.buffer.join('');
},
esc: function(txt) {
if (data.autoescape) {
return h(txt);
Expand Down Expand Up @@ -49,7 +48,7 @@ var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = func
_ref3 = str.split('.');
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
i = _ref3[_i];
if (i.indexOf('#') === 0) {
if (__indexOf.call(i, '#') >= 0) {
id = i.replace('#', '');
} else {
if (i !== '') {
Expand All @@ -72,15 +71,21 @@ var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = func
return text('"');
}
},
render_attrs: function(obj) {
render_attrs: function(obj, prefix) {
var k, v, _results;
if (prefix == null) {
prefix = '';
}
_results = [];
for (k in obj) {
v = obj[k];
if (typeof v === 'boolean' && v) {
v = k;
}
_results.push(v ? text(" " + k + "=\"" + (this.esc(v)) + "\"") : void 0);
if (typeof v === 'function') {
v = "(" + v + ").call(this);";
}
_results.push(typeof v === 'object' && !(v instanceof Array) ? this.render_attrs(v, prefix + k + '-') : v ? text(" " + (prefix + k) + "=\"" + (this.esc(v)) + "\"") : void 0);
}
return _results;
},
Expand Down Expand Up @@ -163,6 +168,15 @@ var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = func
}
return __ck.render_tag(name, idclass, attrs, contents);
};
yield = function(f) {
var old_buffer, temp_buffer;
temp_buffer = [];
old_buffer = __ck.buffer;
__ck.buffer = temp_buffer;
f();
__ck.buffer = old_buffer;
return temp_buffer.join('');
};
h = function(txt) {
return String(txt).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
};
Expand Down Expand Up @@ -209,7 +223,11 @@ var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = func
return text('\n');
}
};
__ck.doctypes = {"5":"<!DOCTYPE html>","default":"<!DOCTYPE html>","xml":"<?xml version=\"1.0\" encoding=\"utf-8\" ?>","transitional":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">","strict":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">","frameset":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">","1.1":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">","basic":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML Basic 1.1//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd\">","mobile":"<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.2//EN\" \"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd\">","ce":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"ce-html-1.0-transitional.dtd\">"};__ck.coffeescript_helpers = "var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; };";__ck.self_closing = ["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr","basefont","frame"];(function(){ul(function() {
__ck.doctypes = {"5":"<!DOCTYPE html>","default":"<!DOCTYPE html>","xml":"<?xml version=\"1.0\" encoding=\"utf-8\" ?>","transitional":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">","strict":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">","frameset":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">","1.1":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">","basic":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML Basic 1.1//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd\">","mobile":"<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.2//EN\" \"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd\">","ce":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"ce-html-1.0-transitional.dtd\">"};__ck.coffeescript_helpers = "var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; };";__ck.self_closing = ["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr","basefont","frame"];return __ck;
}
this.templates["template"] = function anonymous(data) {
var builder = createBuilder.call(this, data);var a,i,li,p,s,th,u,ul;a = function(){return builder.tag('a', arguments);};i = function(){return builder.tag('i', arguments);};li = function(){return builder.tag('li', arguments);};p = function(){return builder.tag('p', arguments);};s = function(){return builder.tag('s', arguments);};th = function(){return builder.tag('th', arguments);};u = function(){return builder.tag('u', arguments);};ul = function(){return builder.tag('ul', arguments);};(function(){
ul(function() {
var guy, _i, _len, _ref, _results;
_ref = this.stooges;
_results = [];
Expand All @@ -218,6 +236,7 @@ var a,i,li,p,s,th,u,ul;a = function(){return __ck.tag('a', arguments);};i = func
_results.push(li(guy));
}
return _results;
});}).call(data);return __ck.buffer.join('');
});
}).call(data);return builder.compile();
};
}).call(this);
2 changes: 1 addition & 1 deletion examples/browser/regular/build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
coffeekup --js template.coffee
../../../bin/coffeekup --js template.coffee
coffee -c index.coffee

Loading