Hacker News new | ask | show | jobs
by insin 5448 days ago
I started coming to a similar conclusion about pure-JavaScript templating in terms of ease of writing and maintenance after getting most of the way into implementing a templating plugin for my DOMBuilder library, based on this speculative API extracted from a sample Django template: https://gist.github.com/958838

Once you start piling on the mixed, nested content and a mix of elements with and without attributes, it takes some diligence to ensure you've put all your commas and parentheses in the right place first time, and the resulting code isn't all that pleasing on the eye when you compare it to the usual HTML with placeholders for logic/dynamic content.

I still find this style of templating useful for smaller chunks of content like this sort of thing, though:

  function dataTable(headers, objects, properties) {
    return TABLE({cellSpacing: 1, 'class': 'data sortable'}
    , THEAD(TR(TH.map(headers)))
    , TBODY(TR.map(objects, function(obj, attrs, loop) {
        attrs['class'] = (loop.index % 2 == 0 ? 'odd', 'even')
        return TD.map(properties, function(prop) {
          return obj[prop]
        })
      }))
    )
  }