Giter Club home page Giter Club logo

nanohtml's Introduction

nanohtml

npm version build status downloads js-standard-style

HTML template strings for the Browser with support for Server Side Rendering in Node.

Installation

$ npm install nanohtml

Usage

Browser

var html = require('nanohtml')

var el = html`
  <body>
    <h1>Hello planet</h1>
  </body>
`

document.body.appendChild(el)

Node

Node doesn't have a DOM available. So in order to render HTML we use string concatenation instead. This has the fun benefit of being quite efficient, which in turn means it's great for server rendering!

var html = require('nanohtml')

var el = html`
  <body>
    <h1>Hello planet</h1>
  </body>
`

console.log(el.toString())

Interpolating unescaped HTML

By default all content inside template strings is escaped. This is great for strings, but not ideal if you want to insert HTML that's been returned from another function (for example: a markdown renderer). Use nanohtml/raw for to interpolate HTML directly.

var raw = require('nanohtml/raw')
var html = require('nanohtml')

var string = '<h1>This a regular string.</h1>'
var el = html`
  <body>
    ${raw(string)}
  </body>
`

document.body.appendChild(el)

Attaching event listeners

var html = require('nanohtml')

var el = html`
  <body>
    <button onclick=${onclick}>
      Click Me
    </button>
  </body>
`

document.body.appendChild(el)

function onclick (e) {
  console.log(`${e.target} was clicked`)
}

Static optimizations

Parsing HTML has significant overhead. Being able to parse HTML statically, ahead of time can speed up rendering to be about twice as fast.

Browserify

From the command line

$ browserify -t nanohtml index.js > bundle.js

Programmatically

var browserify = require('browserify')
var nanohtml = require('nanohtml')
var path = require('path')

var b = browserify(path.join(__dirname, 'index.js'))
  .transform(nanohtml)

b.bundle().pipe(process.stdout)

In package.json

{
  "name": "my-app",
  "private": true,
  "browserify": {
    "transform": [
      "nanohtml"
    ]
  },
  "dependencies": {
    "nanohtml": "^1.0.0"
  }
}

Webpack

At the time of writing there's no Webpack loader yet. We'd love a contribution!

Babel / Parcel

Add nanohtml to your .babelrc config.

Without options:

{
  "plugins": [
    "nanohtml"
  ]
}

With options:

{
  "plugins": [
    ["nanohtml", {
      "useImport": true
    }]
  ]
}

Options

  • useImport - Set to true to use import statements for injected modules. By default, require is used. Enable this if you're using Rollup.
  • appendChildModule - Import path to a module that contains an appendChild function. Defaults to "nanohtml/lib/append-child".

Attributions

Shout out to Shama and Shuhei for their contributions to Bel, yo-yoify and pelo. This module is based on their work, and wouldn't have been possible otherwise!

See Also

License

MIT

nanohtml's People

Contributors

shama avatar yoshuawuyts avatar goto-bus-stop avatar bcomnes avatar jondashkyle avatar tornqvist avatar andyogo avatar tgfjt avatar toddself avatar ungoldman avatar chromakode avatar izumisy avatar greghuc avatar kemitchell avatar graforlock avatar mauriciopoppe avatar him2him2 avatar stevemao avatar yerkopalma avatar

Watchers

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