htmlr
- Htmlr is a template language for Express.
- Htmlr is an easy way to create DOM elements in the browser.
- Htmlr is a Domain Specific Language (DSL) in javascript for generating HTML.
Htmlr can be used on the server or in the browser. In the browser it can
generate HTML text for use with .innerHTML
or it can generate document
fragments for use with .appendChild
. Template files use a .htmlr
extension
and can be a javascript expression or a function that returns an Htmlr object.
Installation
npm install htmlr
For command line usage, install globaly:
npm install htmlr -g
Browser Usage
For use in the browser, include the following script tag:
Then create your dynamic elements in the browser:
with Htmlr var template = ; var data = name: 'Scott' email: 'scott@example.com'; // html text generationdocumentinnerHTML = template; // DOM object generationdocument;
Name: ScottEmail: scott@example.com
Express Usage
Create the following 2 template files to mimic the default express jade templates and put them in the views directory:
-
layout.htmlr
-
index.htmlr
-
error.htmlr
Then modify the app.js
file to change the default rendering engine to htmlr:
// Configuration app;
Command Line Usage
Htmlr can also be used on the command line to test templates without an express
application running. Create a filed called template.htmlr
and a file with
json data called data.json
:
-
template.htmlr
-
data.json
Then run the following command:
htmlr template -d data.json -l
My Title Hello World! <!--woot!--> My Content
-
Use the
with
statement to prevent pollution of the global namespace in the browser. (with
is forbidden in strict mode :( )with Htmlrvar template = ;var html = template;
Features
-
Use an object literal as the first parameter to set attributes
with Htmlrvar template = ;var html = template; -
Use any other data type for the first parameter and all data types afterward for child nodes
with Htmlrvar template = ;var html = template;Literal String1337 -
Chain objects together to create siblings
with Htmlrvar template = ;var html = template; -
Create templates that can be reused. Pass data structures to templates to ease variable generation
with Htmlrvar template =var data1 = id: 'one' class: 'first second' content: 'Hello';var data2 = id: 'two' class: 'third' content: 'World!';var html = template + template;HelloWorld! -
Use substitution syntax for creating templates that can be fed data, either objects or arrays
with Htmlrvar template1 = ;var data1 = name: 'Scott';var html1 = template1;Scottwith Htmlrvar template2 = ;var data2 = 'Scott';var html2 = template2;Scott -
Loop through data structures, objects or arrays, using the
each
constructwith Htmlrvar template1 =;var data1 = 'one' 'two' 'three';var html1 = template1;onetwothreewith Htmlrvar template2 =;var data2 = 1: 'one' 2: 'two' 3: 'three';var html2 = template2;1: one2: two3: threeeach
can also take a static list:with Htmlrvar template =;}var html = template;NorthSouthEastWest -
Includes the ability to extracts parts of the data object
with Htmlrvar template1 =;var data1 = error: number: 42 message: 'unknown question';var html1 = template1;Error 42: unknown questionextract
can reach into multiple levels of data structurewith Htmlrvar template2 = ;var data2 = 0 1 2 3 4 5 6 7;var html = template2;4
Known Issues
None yet. We need more testers!