html-maker
Minimalistic HTML builder based on space-pen. Intended to be used from CoffeeScript.
Installation
npm install html-maker --save
Examples:
Here's a CoffeeScript example. The html is generated by a function
that is evaluated in the scope of an HtmlMaker instance, so
it has access to methods for each HTML element through this
:
HtmlMaker = require 'html-maker'= -> @h1 'Greetings' @div class: 'x'=> @span class: 'first''hi' @span class: 'second''there!' @p 'paragraph' @p => @text 'yet' @b 'another' @text 'paragrah'html = HtmlMakerrender view
The generated HTML text will be as follows (albeit without the indentation):
Greetings hi there!paragraphyetanotherparagrah
Instead of evaluating the html-building closure in the
scope of an HtmlMaker (i.e. having the builder in this
),
the builder can be passed as an argument to the closure
by using render_external
instead of render
:
HtmlMaker = require 'html-maker'= htmlh1 'Greetings' htmldiv class: 'x'-> htmlspan class: 'first''hi' htmlspan class: 'second''there!' htmlp 'paragraph' htmlp -> htmltext 'yet' htmlb 'another' htmltext 'paragrah'html = HtmlMakerrender_external view
This form is more amenable to be used from JavaScript, but still not as nice:
var Maker = ;var { html; html; html; html;};var html = Maker;
Partial view helpers
Helper methods that generate partial view can be used with
the render
method:
= @p => @text name @text ' ' @input type: 'text'name: name = @form action: action=> for field in fields @render form_fieldfield @input type: 'submit'value: 'Submit' form_html = HtmlMakerrender form'send.address.com''name''address'
Result (indented):
name address
If the alternative style using an HtmlMaker
argument is desired
we could have written:
= Hp -> Htext name Htext ' ' Hinput type: 'text'name: name = Hform action: action-> for field in fields form_field Hfield Hinput type: 'submit'value: 'Submit' form_html = HtmlMakerrender_external form'send.address.com''name''address'
Note the differences:
- No fat arrows needed (for HtmlMaker's shake, maybe they're needed for other reason)
- No internal
render
method needed
Data attributes
Data attributes can be defined in a single data
object:
= @div data: name: namecity: city => @text nameconsolelog HtmlMakerrender card'John''Springfield'
Result:
John
Which is equivalent to:
= @div 'data-name': name'data-city': address=> @text name
Note that data attribute names can use camelCase:
= @div data: personName: name => @text nameconsolelog HtmlMakerrender card'John'
And they will be automaticalley dasherized:
John
Style attributes
Like data
, style
attributes can be defined with an object
(and property names can be camelized as well):
= @div name data: personName: name style: textAlign: 'center' lineHeight: '100px' border: '10px solid blue'consolelog HtmlMakerrender card'John'
The result (formatted for convenience):
John