Giter Club home page Giter Club logo

domjs's Introduction

domjs

Client and server side dom template engine

Build dom structure easy way with plain JavaScript. Can be used on both client and server side.

Installation

$ npm install domjs

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Usage

What would be the easiest, most intuitive way to build html5 DOM tree with plain JavaScript ?

var mytemplate = function () {
  header(
    h1('Heading'),
    h2('Subheading'));

  nav(
    ul({ 'class': 'breadcrumbs' },
      li(a({ href: '/' }, 'Home')),
      li(a({ href: '/section/'}, 'Section')),
      li(a('Subject'))));

  article(
    p('Lorem ipsum...'));

  footer('Footer stuff');
};

This is how templates for domjs can be written.

Plain domjs usage example:

var domjs = require('domjs')(document);

var ns = domjs.ns;
var dom = domjs.collect(function () {
  ns.header(
    ns.h1('Heading'),
    ns.h2('Subheading'));

  ns.nav(
    ns.ul({ 'class': 'breadcrumbs' },
      ns.li(a({ href: '/' }, 'Home')),
      ns.li(a({ href: '/section/'}, 'Section')),
      ns.li(a('Subject'))));

  ns.article(
    ns.p('Lorem ipsum...'));

  ns.footer('Footer stuff');
});

document.body.appendChild(dom); // Insert generated DOM into document body

To use domjs functions literally as in first example, you will need to prepare dedicated function wrapper (either programmatically or manually) as e.g. following:

var myTemplate = (function () {}
  var { article, footer, h1, h2, header, li, nav, p, ul } = ns;
  return function () {
    header(
      h1('Heading'),
      h2('Subheading'));

    nav(
      ul({ 'class': 'breadcrumbs' },
        li(a({ href: '/' }, 'Home')),
        li(a({ href: '/section/'}, 'Section')),
        li(a('Subject'))));

    article(
     p('Lorem ipsum...'));

    footer('Footer stuff');
  };
}());
var dom = domjs.collect(myTemplate);

Other notes

You can create custom elements:

var myCustomElement = domjs.ns.myCustomElement = domjs.ns.element.bind(domjs, 'my-custom-element');

Optionally you may also provide some custom constructor or methods for that element:

// This has to be defined before any `my-custom-element` is created by domjs
require('domjs/ext')['my-custom-element'] = {
  _construct: function (/* constuctor args */) {},
  methodA: function () { ... }
};

You can save references to elements and operate on them later:

var myul = ul(li('one'), li('two'), li('three'));

// ... some code ...

// add extra items to myul
myul(li('four'), li('five'));

// append myul into other element
div(myul);

You can access DOM elements directly, just invoke returned function with no arguments

(myul() instanceof DOMElement) === true

Comment type nodes:

comment('my comment');

CDATA type nodes

cdata('cdata text');

Text nodes in main scope:

text('my text');

Elements with names that are reserved keywords in JavaScript language, like 'var', should be created with preceding underscore added to its name:

_var('var content');

Tests Build Status

$ npm test

domjs's People

Contributors

medikoo avatar

Watchers

James Cloos avatar

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.