Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-ejs-layouts

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-ejs-layouts - npm Package Compare versions

Comparing version 0.3.1 to 1.0.0

112

lib/express-layouts.js

@@ -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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc