Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 32 lines (28 sloc) 0.999 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
_ = require 'underscore'
BeaParser = require('./beaparser').BeaParser
fs = require 'fs'
debugIt = require('./debugIt').debugIt

#Holds all templates used in the bea process
class TemplateManager
constructor: (fileName = 'code-templates.bea') ->
str = fs.readFileSync fileName, 'ascii'
throw 'Template load failed: #{fileName}' if str.length == 0
@parser = new BeaParser true
try
@parser.parse str
catch e
throw 'Template load failed #{fileName}: ' + e

getTemplate: (name) ->
childNode = @parser.root.findChild(name);

if childNode then return childNode.toString()
return ""
#replaces all variables in the template (keys in obj) with the values in obj
render: (name, obj) ->
if name.match(/^template\s+/) == null then name = 'template ' + name
str = @getTemplate name
return "" unless str.length > 0
_.each obj, (val, key) ->
if _.isArray val then val = val.join '\n'
str = str.split('$' + key).join(val);
str

exports.TemplateManager = TemplateManager
Something went wrong with that request. Please try again.