UNPKG

creamer

Version:

A flatiron plugin for dynamic coffeecup templates

117 lines (109 loc) 3.13 kB
// Generated by CoffeeScript 1.3.3 var cc, fs, hardcode, wrench; cc = require('coffeecup'); hardcode = require('coffeecup-helpers'); fs = require('fs'); wrench = require('wrench'); exports.attach = function(options) { var controller, fn, helper, items, match, name, registeredViews, self, valid, view, _i, _j, _k, _len, _len1, _len2, _ref; if (options == null) { options = {}; } self = this; registeredViews = {}; if (options.views != null) { items = wrench.readdirSyncRecursive(options.views); for (_i = 0, _len = items.length; _i < _len; _i++) { view = items[_i]; if (view.match(/(.+)\.(js|coffee|coffee\.md)$/)) { fn = require(options.views + '/' + view); name = view.split('.').shift(); registeredViews[name] = fn; } } } if (options.controllers != null) { items = wrench.readdirSyncRecursive(options.controllers); for (_j = 0, _len1 = items.length; _j < _len1; _j++) { controller = items[_j]; if (controller.match(/(.+)\.(js|coffee|coffee\.md)$/)) { fn = require(options.controllers + '/' + controller); this.router.mount(fn); } } } if (options.helpers != null) { items = wrench.readdirSyncRecursive(options.helpers); for (_k = 0, _len2 = items.length; _k < _len2; _k++) { helper = items[_k]; match = helper.match(/(.*)\.(js|coffee|coffee\.md)/); if (match) { fn = require(options.helpers + '/' + helper); name = match[1].replace(/[\. \/]+/g, '_'); valid = cc.compile(fn, { hardcode: hardcode, locals: true }); if (typeof fn === 'function') { hardcode[name] = fn; } } } } this.bind = function(page, data) { var html, _ref; html = ""; if (typeof page === 'string') { page = registeredViews[page]; } if (data == null) { data = {}; } if (((_ref = this.req) != null ? _ref.session : void 0) != null) { data.session = this.req.session; } if ((options.layout != null) && typeof page === 'function') { hardcode.content = page; html = cc.render(options.layout, data, { hardcode: hardcode, locals: true }); } else if (typeof page === 'function') { html = cc.render(page, data, { hardcode: hardcode, locals: true }); } else { '<p>Not Found</p>'; } if (this.res != null) { return this.res.html(html); } else { return html; } }; this.registerHelper = function(name, fn) { valid = cc.compile(fn, { hardcode: hardcode, locals: true }); if (typeof fn === 'function') { return hardcode[name] = fn; } }; this.registerView = function(name, fn) { valid = cc.compile(fn, { hardcode: hardcode, locals: true }); return registeredViews[name] = fn; }; if (((_ref = this.router) != null ? _ref.attach : void 0) != null) { return this.router.attach((function() { return this.bind = self.bind; })); } }; exports.init = function(done) { return done(); };