Comparing version 1.0.1 to 1.0.2
155
index.js
@@ -13,2 +13,3 @@ 'use strict'; | ||
const _exists = fs.existsSync || path.existsSync; | ||
const _normalize = path.normalize; | ||
const _resolve = path.resolve; | ||
@@ -23,12 +24,18 @@ const _extname = path.extname; | ||
// =================== | ||
var moduleRender = module.exports = function(file, options, fn) { | ||
const moduleRender = module.exports = function(file, options, fn) { | ||
options.locals = options.locals || {}; | ||
let settings = options.settings || {}; | ||
settings.layoutsFolder = _normalize(settings.layoutsFolder); | ||
//console.log(options.settings); | ||
//console.log(options); | ||
if (typeof options.locals.blocks) { | ||
// one set of blocks no matter how often we recurse | ||
/*var blocks = { | ||
scripts: new Block(), | ||
stylesheets: new Block() | ||
};*/ | ||
var blocks = { | ||
//scripts: new Block(), | ||
//stylesheets: new Block() | ||
}; | ||
options.locals.blocks = blocks; | ||
@@ -60,7 +67,2 @@ //options.locals.scripts = blocks.scripts; | ||
var layout = options.locals._layoutFile; | ||
// for backward-compatibility, allow options to | ||
// set a default layout file for the view or the app | ||
// (NB:- not called `layout` any more so it doesn't | ||
// conflict with the layout() function) | ||
if (layout === undefined) { | ||
@@ -71,32 +73,11 @@ layout = options._layoutFile; | ||
if (layout) { | ||
// use default extension | ||
var engine = options.settings['view engine'] || 'ejs', | ||
desiredExt = '.' + engine; | ||
// apply default layout if only "true" was set | ||
if (layout === true) { | ||
layout = path.sep + 'layout' + desiredExt; | ||
} | ||
if (extname(layout) !== desiredExt) { | ||
layout += desiredExt; | ||
} | ||
// clear to make sure we don't recurse forever (layouts can be nested) | ||
delete options.locals._layoutFile; | ||
delete options._layoutFile; | ||
// make sure caching works inside ejs.renderFile/render | ||
delete options.filename; | ||
if (layout.length > 0 && layout[0] === path.sep) { | ||
// if layout is an absolute path, find it relative to view options: | ||
layout = join(options.settings.views, layout.slice(1)); | ||
} else { | ||
// otherwise, find layout path relative to current template: | ||
layout = resolve(dirname(file), layout); | ||
} | ||
let layoutSource = _join(settings.layoutsFolder, layout+'.ejs'); | ||
layout = _resolve(layoutSource); | ||
// now recurse and use the current result as `body` in the layout: | ||
options.locals.body = html; | ||
renderFile(layout, options, fn); | ||
moduleRender(layout, options, fn); | ||
} else { | ||
@@ -164,39 +145,15 @@ // no layout, just do the default: | ||
var engine = options.settings['view engine'] || 'ejs', | ||
desiredExt = '.' + engine, | ||
ext = extname(partial) || desiredExt, | ||
key = [root, partial, ext].join('-'); | ||
let settings = options.settings || {}; | ||
let partialSource = _resolve(_normalize(_join(settings.partialsFolder, partial+'.ejs'))); | ||
if (options.cache && cache[key]) return cache[key]; | ||
var key = [root, partial, '.ejs'].join('-'); | ||
// Make sure we use dirname in case of relative partials | ||
// ex: for partial('../user') look for /path/to/root/../user.ejs | ||
var dir = dirname(partial), | ||
base = basename(partial, ext); | ||
if (options.cache && cache[key]) { | ||
return cache[key]; | ||
} | ||
// _ prefix takes precedence over the direct path | ||
// ex: for partial('user') look for /root/_user.ejs | ||
partial = resolve(root, dir, '_' + base + ext); | ||
if (exists(partial)) return options.cache ? cache[key] = partial : partial; | ||
if (_exists(partialSource)) { | ||
return options.cache ? cache[key] = partial : partialSource; | ||
} | ||
// Try the direct path | ||
// ex: for partial('user') look for /root/user.ejs | ||
partial = resolve(root, dir, base + ext); | ||
if (exists(partial)) return options.cache ? cache[key] = partial : partial; | ||
// Try index | ||
// ex: for partial('user') look for /root/user/index.ejs | ||
partial = resolve(root, dir, base, 'index' + ext); | ||
if (exists(partial)) return options.cache ? cache[key] = partial : partial; | ||
// FIXME: | ||
// * there are other path types that Express 2.0 used to support but | ||
// the structure of the lookup involved View class methods that we | ||
// don't have access to any more | ||
// * we probaly need to pass the Express app's views folder path into | ||
// this function if we want to support finding partials relative to | ||
// it as well as relative to the current view | ||
// * we have no tests for finding partials that aren't relative to | ||
// the calling view | ||
return null; | ||
@@ -229,35 +186,39 @@ } | ||
function partial(view, options) { | ||
function partial(view, opts) { | ||
var collection, object, locals, name; | ||
// parse options | ||
if (options) { | ||
// parse opts | ||
if (opts) { | ||
// collection | ||
if (options.collection) { | ||
collection = options.collection; | ||
delete options.collection; | ||
} else if ('length' in options) { | ||
collection = options; | ||
options = {}; | ||
if (opts.collection) { | ||
collection = opts.collection; | ||
delete opts.collection; | ||
} else if ('length' in opts) { | ||
collection = opts; | ||
opts = {}; | ||
} | ||
// locals | ||
if (options.locals) { | ||
locals = options.locals; | ||
delete options.locals; | ||
if (opts.locals) { | ||
locals = opts.locals; | ||
delete opts.locals; | ||
} | ||
// object | ||
if ('Object' != options.constructor.name) { | ||
object = options; | ||
options = {}; | ||
} else if (options.object !== undefined) { | ||
object = options.object; | ||
delete options.object; | ||
if ('Object' != opts.constructor.name) { | ||
object = opts; | ||
opts = {}; | ||
} else if (opts.object !== undefined) { | ||
object = opts.object; | ||
delete opts.object; | ||
} | ||
} else { | ||
options = {}; | ||
opts = {}; | ||
} | ||
let options = { | ||
data: opts | ||
}; | ||
// merge locals into options | ||
@@ -276,3 +237,3 @@ if (locals) | ||
// (FIXME: filename is set by ejs engine, other engines may need more help) | ||
var root = dirname(options.filename), | ||
var root = _dirname(options.filename), | ||
file = lookup(root, view, options), | ||
@@ -283,6 +244,8 @@ key = file + ':string'; | ||
//console.log(cache); | ||
// read view | ||
var source = options.cache ? | ||
cache[key] || (cache[key] = fs.readFileSync(file, 'utf8')) : | ||
fs.readFileSync(file, 'utf8'); | ||
cache[key] = fs.readFileSync(file, 'utf8'); | ||
@@ -408,17 +371,1 @@ options.filename = file; | ||
} | ||
// bound to scripts Block in renderFile | ||
function script(path, type) { | ||
if (path) { | ||
this.append('<script src="' + path + '"' + (type ? 'type="' + type + '"' : '') + '></script>'); | ||
} | ||
return this; | ||
} | ||
// bound to stylesheets Block in renderFile | ||
function stylesheet(path, media) { | ||
if (path) { | ||
this.append('<link rel="stylesheet" href="' + path + '"' + (media ? 'media="' + media + '"' : '') + ' />'); | ||
} | ||
return this; | ||
} |
{ | ||
"name": "ejs-801s", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "W.I.P.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10509
310