Git Product home page Git Product logo

j2h's Introduction

j2h

j2h converts js object/arrary to html string. It's similar to json2html, but a lot simpler, and doesn't require jQuery, and hopefully more intuitive and flexible.

Basic

var json = {
    '<>': 'div',
    html: [
              'this is ', 
              {'<>':'b', html:'important'}
          ]
};
var template = j2h(json); // template is a function itself
template(); // when called it converts the json obj to DOM string.

In the json obj, <> is a special key whose value will be the tag of HTML element, html means 'innerHTML', also a special key for nested elements.

The other special keys are [] and {} as illustrated below.

Render data

To fill data in DOM string, replace values in the json obj with callback functions.

var template = j2h({'<>':'p', style:d=>`background:${d.color}`, html:d=>d.words});
var data = {color:'red', words:'this is a paragraph.'};
template(data);

// or render an array of data
template.batch([{color:'red', words:'paragraph one'},
                {color:'green', words:'paragraph two'}]);

arr2tab(header, array) is a helper function for converting array to a table (a list of obj with same fields).

arr2tab('color/words', 
      [['red','paragraph one'], 
      ['green', 'paragraph two']]);

A few handy callback functions

_0 is predefined to be data=>data[0], also available are _1, _2, _3, _id, and _txt for data=>data.text

var template = j2h({'<>':'a', html:_0, href:_1});
template(['百度', 'www.baidu.com']);

_i and _val are for index and value of the array.

var option_template = j2h({'<>':'option', value:_i, html:_val}).batch;
var select_template = j2h({'<>':'select', html: option_template}); // option_template as a callback funcion
select_template(['English','中文']);

{} and []

By default, callback functions act on the original data, but {} can alter the data.

[] alters the data before it's rendered by inner html template.

var template = j2h({'{}':d=>d.form,
                    '<>':'form', method:f=>f.method, action:f=>f.url,

                    '[]': d=>d.inputs,
                    html: j2h([{'<>':'label', html:_txt}, {'<>':'input', type:i=>i.type}, '<br>']).batch
                });

template({form: {method:'post', url:'/submit.php'},
         inputs: arr2tab('text/type', 
                        [['quantity', 'number'], 
                        ['color', 'color'], 
                        ['urgent', 'checkbox']])});

j2h's People

Contributors

hyansuper avatar

Watchers

 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.