Giter VIP home page Giter VIP logo

domthing's Introduction

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.

domthing's People

Contributors

adamdicarlo avatar floridoo avatar justgage avatar latentflip avatar prust avatar ravinggenius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

domthing's Issues

How to bypass html encoding

Hey, ist there a way to avoid the HTML encoding, so I could do things like

<article>
    <h2>{{title}}</h2>
    {{content}}
</article>

Probably three curly brackets like mustache/handlebars are doing it? {{{content}}}

Really funky expression parsing

The sexp parser is compiling (log model) very bizzarely in some scenarios:

f\u001b[1mf\u001b[22m\u001b[4mf\u001b[24m\u001b[9mf\u001b[29m\u001b[3mf\u001b[23m\u001b[7mf\u001b[27m\u001b[90mf\u001b[39m\u001b[30mf\u001b[39m\u001b[33mf\u001b[39m\u001b[31mf\u001b[39m\u001b[32mf\u001b[39m\u001b[34mf\u001b[39m\u001b[37mf\u001b[39m\u001b[36mf\u001b[39m\u001b[35mf\u001b[39m\u001b[49;5;8mf\u001b[49m\u001b[40mf\u001b[49m\u001b[43mf\u001b[49m\u001b[41mf\u001b[49m\u001b[42mf\u001b

unable to compile templates

Haven't had time to dig through and see what the issue is, but thought I'd open this up anyways.

node v0.10.29
Arch linux

Domthing is not globally installed

meandave@mainframe:~/Code/sequencer$ node_modules/domthing/bin/domthing templates/ > public/templates.js

/home/meandave/Code/sequencer/node_modules/domthing/lib/parser.js:170
    tmpl = tmpl.trim();
                ^
TypeError: Object <li>
  <input data-grid="{{data-grid}}" type="checkbox" />
</li>
 has no method 'trim'
    at Object.parse [as parser] (/home/meandave/Code/sequencer/node_modules/domthing/lib/parser.js:170:17)
    at compileTemplate (/home/meandave/Code/sequencer/node_modules/domthing/bin/domthing:44:14)
    at /home/meandave/Code/sequencer/node_modules/domthing/node_modules/async/lib/async.js:249:17
    at /home/meandave/Code/sequencer/node_modules/domthing/node_modules/async/lib/async.js:125:13
    at Array.forEach (native)
    at _each (/home/meandave/Code/sequencer/node_modules/domthing/node_modules/async/lib/async.js:46:24)
    at async.each (/home/meandave/Code/sequencer/node_modules/domthing/node_modules/async/lib/async.js:124:9)
    at _asyncMap (/home/meandave/Code/sequencer/node_modules/domthing/node_modules/async/lib/async.js:248:13)
    at Object.map (/home/meandave/Code/sequencer/node_modules/domthing/node_modules/async/lib/async.js:219:23)
    at /home/meandave/Code/sequencer/node_modules/domthing/bin/domthing:23:11

Is this still being maintained?

I attempted to use this a while ago, but quickly hit a raft of problems and bugs. I notice the repo has not been touched in six months (as of writing). I find this frustrating because I would very much like to use domthing for my ampersand app - I find the plain-html-plus-bindings style tiresomely verbose, but using interpolation libraries like handlebars requires re-rendering views on every change.

I feel strongly that if this is abandonware it should be removed from the official ampersand docs & examples rather than leading developers down a blind alley.

Only firstChild is returned

When the template is rendered, a nice document fragment is built up, however only the firstChild of that fragment is returned.

This makes it impossible to have multiple top-level elements in your template, even though I don't see any technical limitations.

In some cases the firstChild thing might make sense, but this prevents me from using it in a library like Backbone.Marionette where I already have a top-level element and I don't want to have nested containers before my actual content.

I think it would be better to just return the document fragment itself, or an array of all of its children. Both of these would play very nicely with other tools like jQuery.fn.append

Error: Cannot find helper each

I am trying to use AmpersandJS with domthing to create new app, however, getting the following error:

Error: Cannot find helper each

if (!HELPERS[name]) throw new Error('Cannot find helper ' + name);

Any help is greatly appreaciated.

Parsing and compiling bindings with hyphens?

Doesn't seem to be the cause of #3, but that issue made me wonder how/whether parsing and compiling bindings with hyphens works, a la:

<li>
  <input data-grid="{{data-grid}}" type="checkbox" />
</li>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.