Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/binocarlos/html-include
Process .html files as ejs templates from the document_root
https://github.com/binocarlos/html-include
Last synced: 11 days ago
JSON representation
Process .html files as ejs templates from the document_root
- Host: GitHub
- URL: https://github.com/binocarlos/html-include
- Owner: binocarlos
- License: mit
- Created: 2014-02-24T12:58:23.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-02-24T15:34:16.000Z (over 10 years ago)
- Last Synced: 2024-10-12T17:54:35.632Z (about 1 month ago)
- Language: JavaScript
- Size: 133 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
html-include
============Process .html files as ejs templates from the document_root
## installation
```
$ npm install html-include
```## usage
Create a html-include handler for a folder full of .html files:
```js
var htmlinclude = require('html-include');var app = express();
var includes = htmlinclude({
// the root folder with our .html files
document_root:__dirname + '/www',// allow '/contact' = '/contact.html'
allowdirs:true,
})// this registers the .html -> ejs template handler
includes.setup(app);// we can fill in page specific template variables here
includes.on('page', function(filepath, vars){
if(filepath.match(/\/contact/){
vars.title = 'Contact Page';
}
})// your application routes
app.get('/my/app/etc', function(req, res, next){})
// mount the actual file server
app.use(includes.serve)
```Inside each .html file we can use the 'include' function for things like headers and footers:
## index.html
```html
<% include header.html %>This is the index page
<% include footer.html %>
```## header.html
```html
<%= pagetitle %>
```
## footer.html
```html
```
## page events
each time a template is rendered - the 'page' event is fired with the originating request and a vars object to be populated:
```js
includes.on('page', function(filepath, vars){
if(filepath.match(/\/contact/){
vars.title = 'Contact Page';
}
else if(filepath.match(/\/shop/){
vars.title = 'Shop Page';
}
})
```## async data
you can fetch data asynchronously inside the page event as follows:
```js
includes.on('page', function(filepath, vars, done){
if(filepath.match(/\/async/){// set this flag and the response will wait for 'done' to be called
vars._async = true;load_database_data(function(error, data){
if(error){
return done(error);
}
vars.data = data;
done();
})
}
})
```## render
you can call the render method from elsewhere in your logic:
```js
app.get('/mypath', function(req, res, next){
if(req.params.flag){
includes.render('/myflag.html', {
flag:true
})
}
else{
next();
}
})
```## license
MIT