hem-render-html v0.0.3
hem-render-html
This module adds html rendering support to hem (Bundler for Node/CommonJS/Web Apps). Currently it
only uses .eco templates but in the future I will make it pluggable. I created this module to
allow myself the ability to create my static html pages dynamically. I wanted to be able to create a
header and footer partial template used for all static pages in my site.
Usage
Add
hem-render-htmlandecoto the dependencies in yourpackage.jsonfile in the root directory of your hem project. Here is an example:{ "name": "hem-render-html-example", "version": "0.0.2", "dependencies": { "serveup": "~0.0.4", "hem": "~0.1.7", "eco": "1.1.0-rc-3", "es5-shimify": "~0.0.1", "json2ify": "~0.0.1", "jqueryify": "~0.0.1", "spine": "~1.0.6" } }Once hem-render-html is added to your dependencies run the following command from your project root:
npm install .Add a
slug.jsfile to the root of your project with the following:var hem = new (require('../lib/hem-render-html')); var argv = process.argv.slice(2); hem.exec(argv[0]);Add your html templates and global context file.
Template Files
hem-render-html is setup to generate html files for all files within the html dir that have an
.ecofile extension. The one exception is if the filename starts with two underscores__. This convention allows us to easily create partial templates that can be used in full page templates. For example I commonly have__header.ecoand__footer.ecopartial templates. For a good example of this in a project you can look in theexamplesfolder of this repo.Render Helper
To render another template (most commonly a partial template like
__header.eco) from within another template you use therenderhelper. Here is an example use of therenderhelper with a local context object passed in:<%- @render "__header.eco", {pageName: 'Home'} %>Below is information on the global and local context objects that are available to templates.
Global Context
hem-render-html loads a global context file before it renders an html page. This allows you to set global data and helpers available from all your templates. This file is located in the root of the html folder and is named
__global.coffee. The object exported from this module is then available to all templates.Local Context
When rendering a partial template with the
renderhelper you can pass a local context. This local context holds values specific to the template being rendered. By default these local context values are merged with the global context values (the local context values override global context values when merged).The
renderhelper does have an optional third boolean argumentincludeGlobalContext. This argument is defaultonwhich merges the local context with the global context. If you do not want your partial template's context object to include the global context values you can set this tooff. Whenoffonly the values in the local context are passed to the template. Here is an examplerenderhelper withincludeGlobalContextoff:<%- @render "__footer.eco", {}, off %>Use it! Now when you run hem in your project root it will generate html files for your html templates. For example to bunlde your app run:
hem bundleOr run the development server:
hem server