Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created August 12, 2014 17:01
Show Gist options
  • Save enjalot/226e0c0a35dbcc03c8ae to your computer and use it in GitHub Desktop.
Save enjalot/226e0c0a35dbcc03c8ae to your computer and use it in GitHub Desktop.
derby template rendering for email

email template rendering

==========================

this will use derby-templates to render emails from files.

the controller is just a module with view functions attached to it, it can be an empty object otherwise.

{templates, contexts} = require 'derby-templates'
require 'derby-parsing'
util = require './util'
fs = require 'fs'
cache = {}
exports.createRenderer = (ns, parts, controller = util) ->
views = new templates.Views()
for name, file of parts
path = "#{__dirname}/templates/#{ns}/#{file}"
template = cache[path]
if !template
template = cache[path] = fs.readFileSync(path).toString()
views.register "#{ns}:#{name}", template
contextMeta = new contexts.ContextMeta({})
return (ns, name, context = {}) ->
view = views.find "#{ns}:#{name}"
controller.model = {data: context}
context = new contexts.Context(contextMeta, controller);
return view?.get context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment