Δt - async & dynamic templating engine
dynamictemplate is yet another template engine, but this time loaded with full async support and capable of being changed even after the template was rendered.
It works in browsers too.
$ npm install dynamictemplate
if any of this problems are familiar to you, you should skip the tl;dr and read the documentation:
Convenient DOM manipulation in template style for real-time user interfaces.
Writing and maintaining user interfaces can be hard.
DOM has growen old. It's one of the legacies from the last millenium each browser caries with it.
Like nearly every browser API, the DOM has an ,opinionated, ugly interface to work with:
the fastest way to fill a DOM is
innerHTML, the way to get stuff back once its parsed is by querying it, the convenient way to manipulate it is by changing, creating and removing nodes. so WTF.
These are the reasons why jquery and mootools are still the most used js libraries. but seriously, have you every tried to write an large userinterface with it?
So let's try something new:
var Template = require'dynamictemplate'Template;var tpl = schema:'html5'thisdivthistext"hello world";thisend;;;
require 'dynamictemplate'tpl = schema:'html5'->@div ->@text "hello world"@end
That was easy. We created a new template instance and with it a new
<div> element with some text and closed it.
Let's try something more complex:
return schema:5this$divviewon'set title' thistext;;this$athistext"back";viewon'navigate'thisattr'href' url;bindthis;;;
=schema:'5'->@$div ->viewon'set title'@text@$a href:'/'->@text "back"viewon'navigate' @attrhref:url
Ok. let me explain: we created a div which text changes on every 'set title' event the view object will emit and we created an anchor element which
href attribute will change on every 'navigate' event. that's it.
note that the div element will be empty at the beginning.
if you play a while with it you might hit some known problems from nodejs: flow control. how convenient that it seems that nearly everybody has writting her own library. Use your own flow control library!
if you don't know any, async might be a good fit.
if you already started playing around with it you might found out that nothing is happing. Its because each
this.div call doesn't produce a div tag but a
new and a
add event with the object representation of the div tag as argument. Doesn't sound very useful to you? how about you use one of the many adapters? An Adapter is little modules that listens for these events and act accordingly on its domain. This means if you use dt-jquery or dt-dom it will create a dom element. in the case of dt-stream it will create a nodejs stream instance that emits html strings as data.
var jqueryify = require'dt-jquery';tpl = jqueryifytemplateview;// orvar domify = require'dt-dom';tpl = domifytemplateview;// orvar streamify = require'dt-stream';tpl = streamifytemplateview;
For more information on the events look at asyncxml which generates them.
Let's have another example:
return schema:5this$divclass:'user'var name = thisaclass:'name';var about = thisspanclass:'about';viewon'set user'nametextusername;nameattr'href' userurl;abouttextuserdescription;;setUserviewcurrentUser;aboutend;nameend;;;
=schema:5->@$div class:'user'->name = @aclass:'name'about = @spanclass:'about'=nametextusernamenameattrhref:userurlabouttextuserdescriptionviewon'set user'setUsersetUserviewcurrentUseraboutendnameend
Alright. here is the trick: unlike the DOM where you normally have to query most elements, which feels mostly like grabbing into a black box with spiders and snakes, with Δt you already created the tags, so store them in variables, scopes and/or closures when you need them.
For more information look at the various examples and plugins supporting Δt: