express-ejs-layouts
Advanced tools
Comparing version 0.3.1 to 1.0.0
@@ -1,19 +0,20 @@ | ||
var contentPattern = '&&<>&&' | ||
/*jslint sloppy:true indent:2 plusplus:true regexp:true*/ | ||
var contentPattern = '&&<>&&'; | ||
function contentFor(contentName) { | ||
return contentPattern + contentName + contentPattern | ||
return contentPattern + contentName + contentPattern; | ||
} | ||
function parseContents(locals) { | ||
var str = locals.body | ||
var regex = new RegExp('\n?' + contentPattern + '.+?' + contentPattern + '\n?', 'g') | ||
var split = str.split(regex) | ||
var matches = str.match(regex) | ||
var name, i = 1, str = locals.body, | ||
regex = new RegExp('\n?' + contentPattern + '.+?' + contentPattern + '\n?', 'g'), | ||
split = str.split(regex), | ||
matches = str.match(regex); | ||
locals.body = split[0] | ||
var i = 1; | ||
locals.body = split[0]; | ||
if (matches != null) { | ||
matches.forEach(function(match) { | ||
var name = match.split(contentPattern)[1] | ||
if (matches !== null) { | ||
matches.forEach(function (match) { | ||
name = match.split(contentPattern)[1]; | ||
locals[name] = split[i]; | ||
@@ -26,6 +27,5 @@ i++; | ||
function parseScripts(locals) { | ||
var str = locals.body | ||
locals.script = '' | ||
var regex = /\<script(.|\n)*?\>(.|\n)*?\<\/script\>/g | ||
var str = locals.body, regex = /\<script(.|\n)*?\>(.|\n)*?\<\/script\>/g; | ||
locals.script = ''; | ||
if (regex.test(str)) { | ||
@@ -37,58 +37,50 @@ locals.body = str.replace(regex, ''); | ||
module.exports = exports = function(req, res, next) { | ||
module.exports = function (req, res, next) { | ||
var render = res.render; | ||
res.render = function(view, options, fn) { | ||
var options = options || {}; | ||
// support callback function as second arg | ||
if ('function' == typeof options) { | ||
fn = options, options = {}; | ||
res.render = function (view, options, fn) { | ||
var layout, self = this, app = req.app, | ||
defaultLayout = app.get('layout'); | ||
options = options || {}; | ||
if (typeof options === 'function') { | ||
fn = options; | ||
options = {}; | ||
} | ||
var app = req.app | ||
var defaultLayout = app.get('layout') | ||
var specifiedLayout = options.layout | ||
if (specifiedLayout === false || ((specifiedLayout || defaultLayout) === false) ) { | ||
render.call(res, view, options, fn) | ||
return | ||
if (options.layout === false || ((options.layout || defaultLayout) === false)) { | ||
render.call(res, view, options, fn); | ||
return; | ||
} | ||
var layout = specifiedLayout || defaultLayout | ||
if (layout === true || layout === undefined) | ||
layout = 'layout' | ||
options.contentFor = contentFor | ||
var self = this; | ||
render.call(res, view, options, function(err, str){ | ||
if (err) { | ||
if (fn) | ||
return fn(err) | ||
else | ||
throw err | ||
} | ||
var locals = { body: str } | ||
for (var l in options) { | ||
if (options.hasOwnProperty(l) | ||
&& l != 'layout' | ||
&& l != 'contentFor') | ||
layout = options.layout || defaultLayout; | ||
if (layout === true || layout === undefined) { | ||
layout = 'layout'; | ||
} | ||
options.contentFor = contentFor; | ||
render.call(res, view, options, function (err, str) { | ||
var l, locals; | ||
if (err) { return fn ? fn(err) : next(err); } | ||
locals = { | ||
body: str, | ||
defineContent: function(contentName) { return locals[contentName] || ''; } | ||
}; | ||
for (l in options) { | ||
if (options.hasOwnProperty(l) && l !== 'layout' && l !== 'contentFor') { | ||
locals[l] = options[l]; | ||
} | ||
} | ||
if (options.extractScripts === true | ||
|| (options.extractScripts === undefined && app.get('layout extractScripts') === true)) | ||
parseScripts(locals) | ||
parseContents(locals) | ||
if (options.extractScripts === true || (options.extractScripts === undefined && app.get('layout extractScripts') === true)) { | ||
parseScripts(locals); | ||
} | ||
parseContents(locals); | ||
render.call(self, layout, locals, fn); | ||
}); | ||
}; | ||
next(); | ||
}; |
@@ -5,4 +5,8 @@ { | ||
"description": "Layout support for ejs in express.", | ||
"keywords": ["express", "layout", "ejs"], | ||
"version": "0.3.1", | ||
"keywords": [ | ||
"express", | ||
"layout", | ||
"ejs" | ||
], | ||
"version": "1.0.0", | ||
"main": "lib/express-layouts.js", | ||
@@ -9,0 +13,0 @@ "dependencies": {}, |
@@ -49,3 +49,4 @@ | ||
### script blocks extraction | ||
### Script blocks extraction | ||
If you like to place all the script blocks at the end, you can do it like this: | ||
@@ -79,2 +80,26 @@ | ||
## Optional sections | ||
In a layout, you can have optional sections using `defineContent`: | ||
Unspecified section content defaults to `''`. | ||
1 | ||
<%-defineContent('a')%> | ||
2 | ||
<%-defineContent('b')%> | ||
3 | ||
with a view: | ||
<%- contentFor('a') %> | ||
1.5 | ||
will render: | ||
1 | ||
1.5 | ||
2 | ||
3 | ||
## Running tests | ||
@@ -87,2 +112,2 @@ Clone the rep | ||
MIT | ||
MIT |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5597
1
111
0
67