Giter Site home page Giter Site logo

jext's Introduction

JEXT - Javascript ex DOM templater

JEXT is tool for using html templates as JS files with DOM rendered elements and special helper to manipulate real time data updates.

Installing

To install JEXT you have to just run npm in your project dir:

npm install jext ---save-dev

Usage

Sample usage with bin tool

You have test.html example template. To build it just use jext bin tool:

./bin/jext examples/simple.jext > templates.js

Now merge jext.js lib with generated template

cat ./jext.js templates.js > bundle.js

Well you can do same as above with one command:

./bin/jext examples/simple.jext -b > bundle.js

Finally require in browser just created the bundle.js file. You got jext variable defined with template pool manipulation helpers and templates variable with your template.

Now just get rendered DOM and append to any element. The key of template same as file name without extension.

var t = templates.get('simple');
document.body.appendChild(t.dom());

Wanna update some parameters? Easy:

t.update({welcome_text: 'JEXT works!'});

Now you can see rendered test template on your screen.

Usage with webpack or whatever

First install loader for webpack. Instruction read here: (https://www.npmjs.com/package/jext-loader).

Install stuff:

npm install jext
npm install jext-loader

Somewhere in your code write to render

# First require dependencies
var jext = require('jext');
var simple_tpl = require('examples/simple.jext');

# Make pool of templates, really only one now
var pool = jext.pool(simple_tpl);

# Fetch template and render
var t = pool.get('simple')
document.body.appendChild(t.dom());
t.update({welcome_text: 'JEXT works!'});

Build your project and enjoy!

Template markup

You can use only simple constructs in your html template.

Variables

Just use {{ var_name }} in attribute or inside element to make it works.

<div title="{{ title }}">{{ body }}</div>

Iteration

When making iteration inside template JEXT switching context inside iterated object. So only possible to access object variables of current iterated item.

  {
    "rows": [
      {"value": 1},
      {"value": 2}
    ]
  }
<div for="rows">
  The value is: {{ value }}
</div>

You know what will be as result? :)

Condition

Condition switches context as iteration. Its same easy to use.

  {
    "first": false,
    "second": {
      "value": 2
    }
  }
  <div if="first">
    First block wont appear here
  </div>
  <div if="second">
    In context of second condition: {{ value }}
  </div>

Template pool methods

get(template, data)

Return template object from pool of available templates using the template key.

  • template - name of template. If your template name as mytemplate.jext it must be mytemplate (without extension).
  • data - optional. Pass data to render your template as object.

release(template, instance)

It releases generate template from DOM tree

  • template - name of template.
  • instance - generated Node of this template.

Template object methods

When you get template from created pool, it has several methods to manipulate it.

dom()

The method returns generated DOM tree for current template as DocumentFragment (see document.createDocumentFragment).

update(data)

  • data - object to update and rerender current loaded template's DOM.

remove()

This method removes rendered element from DOM tree.

Loaders for JEXT

Webpack: (https://www.npmjs.com/package/jext-loader).

jext's People

Contributors

dmitrykuzmenkov avatar

Watchers

 avatar  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.