-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtemplateCache.js
More file actions
135 lines (120 loc) · 3.43 KB
/
templateCache.js
File metadata and controls
135 lines (120 loc) · 3.43 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var gulp = require('gulp')
var handlebars = require('gulp-handlebars')
var wrap = require('gulp-wrap')
var declare = require('gulp-declare')
var concat = require('gulp-concat')
var path = require('path')
var diyHbsHelpers = require('diy-handlebars-helpers')
var hbsHelpers = require('./hbs-helpers')
var views = ''
var partials=''
var helpers=''
function TemplateCache() {
this.hbsOptions = {}
}
TemplateCache.prototype.recompile = function(cb) {
views = ''
partials = ''
helpers = ''
return this.compile(cb)
}
TemplateCache.prototype.setHbs = function(hbs) {
this.hbsOptions.handlebars = hbs
}
TemplateCache.prototype.compile = function(cb) {
var that = this
if (partials && helpers && views) {
return cb && cb()
}
var partialPaths = [
__dirname+'/../views/partials/**/*.handlebars'
// server/partials not in cache
]
var sourcePaths = [
__dirname+'/../views/**/*.handlebars',
'!'+__dirname+'/../views/server/**/*.handlebars',
'!'+__dirname+'/../views/layouts/*.handlebars',
'!'+__dirname+'/../views/graph/*.handlebars',
'!'+__dirname+'/../views/*.handlebars'
]
// registers diyHbsHelpers and hbsHelpers to use client-side
function makeHelpers() {
var helpersJs = []
var key
for (key in diyHbsHelpers) {
if (key !== 'templateCache')
helpersJs.push("Handlebars.registerHelper('" + key + "'," + diyHbsHelpers[key].toString()+");")
}
for (key in hbsHelpers) {
helpersJs.push("Handlebars.registerHelper('" + key + "'," + hbsHelpers[key].toString()+");")
}
return helpers = helpersJs.join("\n")
}
function makePartials() {
return gulp.src(partialPaths)
.pipe(handlebars(that.hbsOptions))
.pipe(wrap('Handlebars.registerPartial(<%= processPartialName(file.relative) %>, <%= partialToE2Name(file.relative) %>);', {}, {
imports: {
// makes a name for a partial from its filename, e.g. assets/graphCard
processPartialName: function (fileName) {
var parts = fileName.split('/')
var prefix = ''
if (parts.length > 1) {
parts.pop()
prefix = parts.pop() + '/'
}
var n = prefix + path.basename(fileName, '.js') // gulp
return JSON.stringify(n)
},
// maps a partial to its E2.views.partials precompiled template
partialToE2Name: function (fileName) {
var name = path.basename(fileName, '.js')
fileName = fileName.split('/')
fileName.pop() // name.js
fileName.push(name) // name
return "E2.views.partials." + fileName.join(".")
}
}
}))
.pipe(concat('partials'))
.on('data', function (data) {
partials += data.contents
})
.on('end', function () {
if (cb)
cb()
})
}
function makeTemplates(cb) {
return gulp.src(sourcePaths)
.pipe(handlebars(that.hbsOptions))
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'E2',
noRedeclare: true, // Avoid duplicate declarations
processName: declare.processNameByPath
}))
.pipe(concat('templates'))
.on('data', function (data) {
views += data.contents
})
.on('end', function () {
if (cb)
cb()
})
}
makeHelpers() // sets helpers
makeTemplates(function(){ // sets cache
makePartials(cb) // sets partials
})
// not a bad idea to make this into a file and serve it separately
return this
}
TemplateCache.prototype.helper = function() {
return {
templateCache: function() {
return helpers + views + partials
}
}
}
module.exports.templateCache = new TemplateCache()