Express middleware for blocks in views.
Middleware for Express that adds basic block support to views.
npm install express-blocks
Please note the version you install and only specify that exact version as your dependency while this is in development. Breaking changes are likely!
app.js, register this middleware:
Then in your (top-level) layout, name and add placeholders for your desired
blocks (e.g. in EJS syntax):
<%- blocks.foo %> ... <%- blocks.bar %>
And from your views (or intermediate layouts), add arbitrary HTML to those
blocks via the
<%- block('foo', '<p>Hello world!</p>') %>
You can use the helper
stylesheet() functions to generate
<link rel="stylesheet"> references for you:
<%- script('/path/to/script.js') %> <%- stylesheet('/path/to/stylesheet.js') %>
Which are exposed to (top-level) layouts as blocks named
stylesheets, also aliased globally just like
<%- scripts %> <%- stylesheets %>
As Borat would say, niiice!
Run the included Express example:
Then open your browser to http://localhost:8080/ to see it in action.
You can verify that everything works as expected via:
If you want to submit a pull request, be sure to add a test case, too!
MIT License. Copyright © 2011 Aseem Kishore and Laurie Harper.