-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsmart-express.js
164 lines (142 loc) · 4.38 KB
/
jsmart-express.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict'
var async = require('async')
var findPartials = require('./find-partials')
var fs = require('fs')
var LRU = require('lru-cache')
var jSmart = require('jsmart')
var path = require('path')
// Load a single file, and return the data.
function loadFile (fullFilePath, callback) {
fs.readFile(fullFilePath, 'utf-8', function (err, data) {
if (err) {
return callback(err)
}
return callback(null, data)
})
}
function handleFile (name, file, layoutPath, options, cache, callback) {
var cachedData
if (!options || !options.settings || options.settings['view cache'] !== false) {
cachedData = cache && cache.get(file)
}
if (!cachedData) {
loadFile(file, function (err, fileData) {
if (err) {
return callback(err)
}
if (fileData && layoutPath) {
fileData = '{extends file="' + layoutPath + '"} ' + fileData
}
var partials = findPartials(fileData)
var data = {
name: name,
data: fileData,
partials: partials
}
if (cache) {
cache.set(file, data)
}
return callback(null, data)
})
} else {
return callback(null, cachedData)
}
}
function consolidatePartials (arr) {
var partialsSet = {}
arr.forEach(function (item) {
item.partials.forEach(function (partial) {
partialsSet[partial] = true
})
})
return Object.keys(partialsSet)
}
// Of the partials given, which haven't been loaded yet?
function findUnloadedPartials (partialNames, loadedPartials) {
return partialNames.filter(function (partialName) {
return !(partialName in loadedPartials)
})
}
function loadAllPartials (unparsedPartials, partialsDirectory, options, cache, partials, callback) {
if (!partials) {
partials = {}
}
if (unparsedPartials.length === 0) {
return callback(null, partials)
}
async.map(unparsedPartials, function (partial, next) {
var fullFilePath = path.resolve(partialsDirectory, partial)
return handleFile(partial, fullFilePath, null, options, cache, next)
}, function (err, data) {
if (err) {
return callback(err)
}
data.forEach(function (partialData) {
partials[partialData.name] = partialData.data
})
var consolidatedPartials = consolidatePartials(data)
var partialsToLoad = findUnloadedPartials(consolidatedPartials, partials)
return loadAllPartials(partialsToLoad, partialsDirectory, options, cache, partials, callback)
})
}
function loadTemplateAndPartials (templateFile, partialsDirectory, layoutPath, options, cache, callback) {
handleFile(null, templateFile, layoutPath, options, cache, function (err, partialData) {
if (err) {
return callback(err)
}
return loadAllPartials(partialData.partials, partialsDirectory, options, cache, null, function (err, partials) {
if (err) {
return callback(err)
}
return callback(null, partialData.data, partials)
})
})
}
function render (templatePath, viewDirectory, viewExtension, options, cache, callback) {
var layoutPath = null
if (options && options.settings && options.settings.layout) {
layoutPath = options.settings.layout + '.' + viewExtension
}
loadTemplateAndPartials(templatePath, viewDirectory, layoutPath, options, cache, function (err, template, partials) {
if (err) {
return callback(err)
}
jSmart.prototype.getTemplate = function (name) {
return partials[name]
}
var settings = {}
if (options && options.settings && options.settings['jsmart-settings']) {
settings = options.settings['jsmart-settings']
}
var smarty = new jSmart(template, settings)
callback(err, smarty.fetch(options))
})
}
function renderer (directory) {
var cache = new LRU({
max: 50000,
length: function (item) {
return item.data.length
}
})
var rendererWrapper = function (templatePath, options, callback) {
var viewDirectory
var viewExtension = 'smarty'
if (options && options.settings) {
viewExtension = options.settings['view engine']
viewDirectory = options.settings.views
}
if (directory) {
viewDirectory = directory
}
render(templatePath, viewDirectory, viewExtension, options, rendererWrapper.cache, function (err, data) {
if (err) {
return callback(err)
}
return callback(err, data)
})
}
rendererWrapper.cache = cache
return rendererWrapper
}
module.exports = renderer