• Stars
    star
    214
  • Rank 184,678 (Top 4 %)
  • Language
    JavaScript
  • Created over 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple, fast, & safe, mustache/handlebars like templating engine with data-binding hooks built in.

Deprecation notice

Due to the growth in popularity of react, and my own enjoyment using it, domthing is effectively being unused and unmaintained by me at this point.

If you are interested in picking it up and developing it let me know, otherwise, sorry.


domthing

What is this?

A DOM-aware, mustache/handlebars-like, templating engine. Heavily inspired by HTMLBars.

How do i I use it?

Check out the demo repo at http://github.com/latentflip/domthing-demo

npm install -g domthing

Now create a directory of templates, ending in .dom, like 'foo.dom':

<!-- foo.dom -->
<h1>{{ greeting }}</h1>
domthing path/to/templates > path/to/templates.js

now in your app you can do:

var templates = require('./path/to/templates.js');

document.addEventListener('DOMContentLoaded', function () {

    //render the template (returns a dom node);
    var rendered = templates.foo({ greeting: 'Hello!' });

    //append the node
    document.body.appendChild(rendered);

    //trigger updates to context options:
    setInterval(function () {
        //or whatever
        rendered.update('greeting', 'Goodbye!');
    }, 5000);
});

Why?

Most templating engines are just string interpolation, precompiling this:

<a href="<%= me.url %>">My profile</a>

generates a function like this:

function template(context) {
    return '<a href="' + context.me.url + '">My profile</a>';
}

which you call like this:

someElement.innerHTML = template({ me: { url: 'twitter.com/philip_roberts' } });

This works, but it's not very smart. If you want to update your page if the context data changes you have to rerender the template (slow), or you have to insert css selector hooks everywhere so that you can update specific elements, a la: <a role="profile-link" href="<%= me.url %>">My Profile</a> and then $('[role=profile-link]').text(me.url).

You've also now split the knowledge of where data goes into the dom in the template into two places, once in the template, and once somewhere in JavaScript land. Or you just do it in JavaScript land and your templates look a little empty. You also better hope nobody changes your html in a way that breaks your css selector code, or you'll be sad :(. Also you've now made it harder for frontend devs who might be comfortable editing templates & styling, but less so tweaking css selector bindings, to actually edit where data goes in your templates.

So, what if your template engine actually understood how the dom worked, and actually returned DOM elements:

//warning, code for illustrative purposes only:
function template(context) {
    var element = document.createElement('a');
    element.setAttribute('href', context.me.url);
    element.appendChild(document.createTextNode('My Profile'));
    return element;
}

And now that you had actual references to real elements, you could just bind them to data changes directly, no css selectors required:

//warning, code for illustrative purposes only:
function template(context) {
    var element = document.createElement('a');
    bindAttribute(element, 'href', 'context.me.url'); //updates href when context changes
    element.appendChild(document.createTextNode('My Profile'));
    return element;
}

If you had that you could trivially write templates that did this:

<a class="call-to-action {{model.active}} {{model.linkType}}">Buy Stuff!</a>

and the classes would all just work, and update with the data, or this:

<a class="call-to-action">
    {{#if user.hasBought }}
        BUY MORE!
    {{#else }}
        BUY SOMETHING!
    {{/if }}
</a>

and the output would update as soon as user.hasBought changed.

More Repositories

1

loupe

Visualizing the javascript runtime at runtime
JavaScript
3,031
star
2

violin

Violin
CoffeeScript
262
star
3

bows

Safe, production happy, colourful logging for chrome & firefox - makes reading your logs much easier.
HTML
215
star
4

webpack-unused

Check your frontend code for files/assets that are no longer used.
JavaScript
144
star
5

track-your-damn-time

Because I'm lazy.
JavaScript
77
star
6

dev-cert-authority

Self-signed certs for development
JavaScript
77
star
7

deval

Like eval, but backwards.
JavaScript
62
star
8

building-static-server

A static file server that runs builds and things
JavaScript
22
star
9

explode

Explodes tars, and implodes dirs.
JavaScript
11
star
10

browserify-grunt-workspaces-demo

JavaScript
9
star
11

hapi_primus_sessions

JavaScript
7
star
12

bacon-talk-realtimeconfeu

JavaScript
7
star
13

domthing-demo

JavaScript
6
star
14

hapi-auth-action-token

JavaScript
6
star
15

stylizer

Simple stylus api for moonboots etc.
JavaScript
6
star
16

LearningD3

JavaScript
5
star
17

d3

JavaScript
5
star
18

domthingify

Browserify transform for domthing.
JavaScript
4
star
19

async-data-loader

JavaScript
4
star
20

timetime

JavaScript
4
star
21

websocket-debug

JavaScript
4
star
22

RedisWeb

A basic web interface for redis.
Python
4
star
23

browserify-eco

JavaScript
4
star
24

eventloop-book

JavaScript
4
star
25

atom-talky-test

CoffeeScript
4
star
26

jade-to-jsx

Generate react components from jade files
JavaScript
4
star
27

active_support_js

CoffeeScript
3
star
28

Tumblog

Styles and Writings for my tumblog
Python
3
star
29

snake

CoffeeScript
3
star
30

scales

JavaScript
3
star
31

BigDecimal.js

Arbitrary-precision decimals library for JavaScript
JavaScript
3
star
32

game-of-life-lua

Game of life in lua
Lua
3
star
33

node-glob-cli

JavaScript
2
star
34

ScotRubyConf

Ruby
2
star
35

weevil

WebWorker eval.
JavaScript
2
star
36

heroku-screengrabber

JavaScript
2
star
37

foundation-stylus

CSS
2
star
38

latentflip.github.com

My blog.
JavaScript
2
star
39

tracker-roadmap

Product roadmap generated automatically from Pivotal Tracker data
Ruby
2
star
40

sinatra-heroku-boilerplate

A simple Sinatra framework app for Heroku deployment, mainly for the purpose of rapid prototyping. Incorporates HTML5 Boilerplate, with HAML, SASS and basic DataMapper config.
JavaScript
2
star
41

beats

JavaScript
2
star
42

geodb

This is a hacky project to make a really basic nosql like geo database. You most probably don't want to use it, unless you're me ;)
JavaScript
2
star
43

Mowars

Python
2
star
44

underscore-koans

underscore-koans
JavaScript
1
star
45

Inflation

JavaScript
1
star
46

joyrtc

JavaScript
1
star
47

rebase

1
star
48

Projects

JavaScript
1
star
49

scotlandjs

JavaScript
1
star
50

Bash-Config

Personal bash config
Vim Script
1
star
51

flood-video

JavaScript
1
star
52

react-jade-example-wut

JavaScript
1
star
53

base32-crockford-browser

JavaScript
1
star
54

kerblam-online

JavaScript
1
star
55

scotruby

Ruby
1
star
56

make-better

Makefile
1
star
57

code-split

JavaScript
1
star
58

tape-suite

Simple suites for tape.
JavaScript
1
star
59

7li7w

Seven languages in seven weeks
Io
1
star
60

erroreteg

This is just silly.
JavaScript
1
star
61

CHS11

Culture Hack Scotland
JavaScript
1
star
62

how-much

CoffeeScript
1
star
63

user-flow

JavaScript
1
star
64

heroku-lockdown

Ruby
1
star
65

JellyTalk-Showoff

Ruby
1
star
66

banking

Ruby
1
star
67

install-deps

JavaScript
1
star
68

conway

JavaScript
1
star
69

mockup

Template for quick wireframe mockups.
Ruby
1
star
70

example-site

JavaScript
1
star
71

sort_test

Ruby
1
star
72

Citipedia--SIcamp-

JavaScript
1
star
73

blogtastic

Blog app using Rails
Ruby
1
star
74

SimpleDecimal.js

CoffeeScript
1
star
75

virtual_attribute

Virtual Attributes that play nice with forms & active_record
Ruby
1
star
76

scottish-ruby-conf-2013-talk

1
star
77

redux-game

JavaScript
1
star
78

boring

JavaScript
1
star
79

watchify-server

JavaScript
1
star
80

BioInf

Python
1
star
81

hjs-webpack-example

JavaScript
1
star
82

stack-queue-visualisation

An attempt to visualise the JavaScript runtime
JavaScript
1
star
83

lytic

JavaScript
1
star
84

bookstore

1
star
85

BBBPAPP

JavaScript
1
star
86

advent-of-code

Elixir
1
star
87

bacon_js_spreadsheet

JavaScript
1
star
88

Ruby-Quizzes

Quizzes from the Ruby Quiz book
Ruby
1
star
89

xmpp-devtools

JavaScript
1
star
90

Mona-Lisa

Matlab genetic algorithm 101
Objective-C
1
star
91

level-tree-db

A leveldb backed tree structure.
JavaScript
1
star
92

Canvas

Canvas is a simple drawing application that allows you to quickly sketch ideas and create drawings. Pen pressure is simulated using touch/mouse velocity, helping to give line drawings a more natural feeling.
JavaScript
1
star
93

Fourscore

JavaScript
1
star