hi

html-include

Process .html files as ejs templates from the document_root

Showing:

Popularity

Downloads/wk

0

GitHub Stars

0

Maintenance

Last Commit

8yrs ago

Contributors

0

Package

Dependencies

2

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

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:

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

<% include header.html %>

This is the index page

<% include footer.html %>

header.html

<html>
    <head>
        <title><%= pagetitle %></title>
    </head>
<body>

footer.html

</body>
</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:

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:

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:

app.get('/mypath', function(req, res, next){
    if(req.params.flag){
        includes.render('/myflag.html', {
            flag:true
        })
    }
    else{
        next();
    }
})

license

MIT

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial