express-ejs-layouts
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -44,2 +44,11 @@ /*jslint sloppy:true indent:2 plusplus:true regexp:true*/ | ||
function parseMetas(locals) { | ||
var str = locals.body, regex = /\<meta(.|\n)*?\>/g; | ||
if (regex.test(str)) { | ||
locals.body = str.replace(regex, ''); | ||
locals.meta = str.match(regex).join('\n'); | ||
} | ||
} | ||
module.exports = function (req, res, next) { | ||
@@ -94,2 +103,7 @@ var render = res.render; | ||
locals.meta = ''; | ||
if (options.extractMetas === true || (options.extractMetas === undefined && app.get('layout extractMetas') === true)) { | ||
parseMetas(locals); | ||
} | ||
parseContents(locals); | ||
@@ -96,0 +110,0 @@ render.call(self, layout, locals, fn); |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"main": "lib/express-layouts.js", | ||
@@ -22,5 +22,2 @@ "dependencies": {}, | ||
"optionalDependencies": {}, | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"scripts": { | ||
@@ -27,0 +24,0 @@ "test": "make test" |
220
Readme.md
@@ -1,49 +0,90 @@ | ||
# express-ejs-layouts | ||
#### *Layout support for ejs in express.* | ||
> Layout support for ejs in express | ||
[![build status](https://secure.travis-ci.org/Soarez/express-ejs-layouts.png)](http://travis-ci.org/Soarez/express-ejs-layouts) | ||
[![build status](https://secure.travis-ci.org/Soarez/express-ejs-layouts.svg)](http://travis-ci.org/Soarez/express-ejs-layouts) | ||
## Installation | ||
npm install express-ejs-layouts | ||
```sh | ||
$ npm install express-ejs-layouts | ||
``` | ||
## Usage | ||
var express = require('express') | ||
, app = express() | ||
, expressLayouts = require('express-ejs-layouts') | ||
app.set('view engine', 'ejs') | ||
app.set('layout', 'myLayout') // defaults to 'layout' | ||
app.use(expressLayouts) | ||
app.use(app.router) | ||
app.get('/', function(req, res){ | ||
res.render('aView', { layout: 'someSpecificLayout' }) | ||
}) | ||
```js | ||
var express = require('express') | ||
, app = express() | ||
, expressLayouts = require('express-ejs-layouts') | ||
app.listen(3000) | ||
app.set('view engine', 'ejs') | ||
app.set('layout', 'myLayout') // defaults to 'layout' | ||
### contentFor | ||
app.use(expressLayouts) | ||
app.use(app.router) | ||
app.get('/', function(req, res){ | ||
res.render('aView', { layout: 'someSpecificLayout' }) | ||
}) | ||
app.listen(3000) | ||
``` | ||
### `contentFor` | ||
A view | ||
somebody | ||
<%- contentFor('foo') %> | ||
club | ||
<%- contentFor('bar') %> | ||
fight | ||
```ejs | ||
somebody | ||
<%- contentFor('foo') %> | ||
club | ||
<%- contentFor('bar') %> | ||
fight | ||
``` | ||
With a layout | ||
<%-bar%> <%-foo%> | ||
<%-body%> | ||
```ejs | ||
<%-bar%> <%-foo%> | ||
<%-body%> | ||
``` | ||
Renders | ||
fight club | ||
somebody | ||
``` | ||
fight club | ||
somebody | ||
``` | ||
As another example, consider this view: | ||
```html | ||
foo | ||
<%- contentFor('pageSectionA') %> | ||
bar | ||
<%- contentFor('pageSectionB') %> | ||
baz | ||
``` | ||
Using it with this layout: | ||
```html | ||
<div class="header"><%- pageSectionA %></div> | ||
<div class="body"><%- body %></div> | ||
<div class="footer"><%-defineContent('pageSectionB')%></div> | ||
``` | ||
Will render: | ||
```html | ||
<div class="header">bar</div> | ||
<div class="body">foo</div> | ||
<div class="footer">baz</div> | ||
``` | ||
Notice that the difference between using `<%- pageSectionA %>` and `<%-defineContent('pageSectionA')%>` is that the former will generate an error if the view doesn't define content for this section. | ||
### Script blocks extraction | ||
@@ -53,28 +94,74 @@ | ||
app.set("layout extractScripts", true) | ||
```js | ||
app.set("layout extractScripts", true) | ||
``` | ||
A view | ||
something<script>somejs<script>something | ||
```html | ||
something<script>somejs<script>something | ||
``` | ||
With a layout | ||
... | ||
<body> | ||
<%- body %> | ||
<%- script %> | ||
</body> | ||
```ejs | ||
<body> | ||
<%- body %> | ||
<%- script %> | ||
</body> | ||
``` | ||
Renders | ||
... | ||
<body> | ||
somethingsomething | ||
<script>somejs<script> | ||
</body> | ||
```ejs | ||
<body> | ||
somethingsomething | ||
<script>somejs<script> | ||
</body> | ||
``` | ||
Enabling invididually: | ||
req.render('view', { extractScripts: true }) | ||
```js | ||
req.render('view', { extractScripts: true }) | ||
``` | ||
When the `"layout extractScripts"` option is activated, scripts defined in views will be extracted (won't be a part of `body`) and will be available for use in the layout through the variable `scripts`. | ||
Another example: | ||
This view: | ||
```html | ||
<script src="/b.js" /> | ||
<div>foo</div> | ||
<script src="/a.js" /> | ||
<div>bar</div> | ||
<script src="/c.js" /> | ||
``` | ||
Used with this layout: | ||
```html | ||
<div class="main"> | ||
<%- body %> | ||
</div> | ||
<!-- place the scripts at the end of the html page --> | ||
<%- script %> | ||
``` | ||
Will render: | ||
```html | ||
<div class="main"> | ||
<div>foo</div> | ||
<div>bar</div> | ||
</div> | ||
<!-- place the scripts at the end of the html page --> | ||
<script src="/b.js" /> | ||
<script src="/a.js" /> | ||
<script src="/c.js" /> | ||
``` | ||
### Style blocks extraction | ||
@@ -84,6 +171,14 @@ | ||
* Supported tags are ``<link rel="stylesheet" …>`` and ``<style …>`` | ||
* The option is named ``extractStyles`` | ||
* The template variable in layout is ``style`` | ||
* Supported tags are `<link rel="stylesheet" …>` and `<style …>` | ||
* The option is named `extractStyles` | ||
* The template variable in layout is `style` | ||
### Meta blocks extraction | ||
Works exactly like script blocks extraction except: | ||
* Supported tags are `<meta …>` and `<meta …/>` | ||
* The option is named `extractMetas` | ||
* The template variable in layout is `meta` | ||
## Optional sections | ||
@@ -94,28 +189,37 @@ | ||
```ejs | ||
1 | ||
<%-defineContent('a')%> | ||
2 | ||
<%-defineContent('b')%> | ||
3 | ||
``` | ||
1 | ||
<%-defineContent('a')%> | ||
2 | ||
<%-defineContent('b')%> | ||
3 | ||
with a view: | ||
<%- contentFor('a') %> | ||
1.5 | ||
```ejs | ||
<%- contentFor('a') %> | ||
1.5 | ||
``` | ||
will render: | ||
1 | ||
1.5 | ||
2 | ||
3 | ||
```ejs | ||
1 | ||
1.5 | ||
2 | ||
3 | ||
``` | ||
## Running tests | ||
Clone the rep | ||
make test | ||
Clone the rep and run: | ||
```sh | ||
$ make test | ||
``` | ||
## License | ||
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
8161
89
223