All Projects → hyperhype → Hyperscript

hyperhype / Hyperscript

Licence: mit
Create HyperText with JavaScript.

Programming Languages

HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Hyperscript

Askama
Type-safe, compiled Jinja-like templates for Rust
Stars: ✭ 1,255 (-45.72%)
Mutual labels:  templating
Genesis
Templating, scaffolding and generation tool
Stars: ✭ 122 (-94.72%)
Mutual labels:  templating
Phptal
PHP Template Attribute Language — template engine for XSS-proof well-formed XHTML and HTML5 pages
Stars: ✭ 155 (-93.3%)
Mutual labels:  templating
Metalsmith React Templates
A metalsmith plugin to render files using React / Preact / JSX based templates.
Stars: ✭ 90 (-96.11%)
Mutual labels:  templating
Plot
A DSL for writing type-safe HTML, XML and RSS in Swift.
Stars: ✭ 1,722 (-25.52%)
Mutual labels:  templating
Jxls
Java library for creating Excel reports using Excel templates
Stars: ✭ 128 (-94.46%)
Mutual labels:  templating
Pebble Intellij
Pebble support for IntelliJ IDEA
Stars: ✭ 68 (-97.06%)
Mutual labels:  templating
Drone Gke
Drone plugin for deploying containers to Google Kubernetes Engine (GKE)
Stars: ✭ 159 (-93.12%)
Mutual labels:  templating
Metalsmith Layouts
🌼 A metalsmith plugin for layouts
Stars: ✭ 109 (-95.29%)
Mutual labels:  templating
Docxtemplater
Generate docx pptx and xlsx (Microsoft Word, Powerpoint, Excel documents) from templates, from Node.js, the Browser and the command line / Demo: https://www.docxtemplater.com/demo
Stars: ✭ 1,990 (-13.93%)
Mutual labels:  templating
Vuerecipe
A recipe for using Buffalo & Vue.js
Stars: ✭ 95 (-95.89%)
Mutual labels:  templating
Htm.py
JSX-like syntax in plain Python
Stars: ✭ 102 (-95.59%)
Mutual labels:  templating
Blade
Use Blade templates without the full Laravel framework
Stars: ✭ 132 (-94.29%)
Mutual labels:  templating
Gomplate
A flexible commandline tool for template rendering. Supports lots of local and remote datasources.
Stars: ✭ 1,270 (-45.07%)
Mutual labels:  templating
Proji
A powerful cross-platform CLI project templating tool.
Stars: ✭ 156 (-93.25%)
Mutual labels:  templating
Fluidcontent
TYPO3 extension Fluidcontent: Fluid Content Element Engine
Stars: ✭ 82 (-96.45%)
Mutual labels:  templating
Tempy
Python Object Oriented Html Templating System
Stars: ✭ 126 (-94.55%)
Mutual labels:  templating
Cruft
Allows you to maintain all the necessary cruft for packaging and building projects separate from the code you intentionally write. Built on-top of, and full compatible with, CookieCutter.
Stars: ✭ 180 (-92.21%)
Mutual labels:  templating
Mag.js
MagJS - Modular Application Glue
Stars: ✭ 157 (-93.21%)
Mutual labels:  templating
Mimic
mimic: Define your Deployments, Infrastructure and Configuration as a Go Code 🚀
Stars: ✭ 145 (-93.73%)
Mutual labels:  templating

HyperScript

Create HyperText with JavaScript, on client or server.

testling badge

Interactive Demo

See also mercury, a modular ui framework influenced by hyperscript but much more heavily optimized.

Example

var h = require('hyperscript')
h('div#page',
  h('div#header',
    h('h1.classy', 'h', { style: {'background-color': '#22f'} })),
  h('div#menu', { style: {'background-color': '#2f2'} },
    h('ul',
      h('li', 'one'),
      h('li', 'two'),
      h('li', 'three'))),
    h('h2', 'content title',  { style: {'background-color': '#f22'} }),
    h('p',
      "so it's just like a templating engine,\n",
      "but easy to use inline with javascript\n"),
    h('p',
      "the intention is for this to be used to create\n",
      "reusable, interactive html widgets. "))

On the server

You can still use hyperscript on the server, the limitation is that events don't make sense anymore, but you can use it to generate html:

console.log(h('h1', 'hello!').outerHTML)
=> '<h1>hello!</h1>'

h (tag, attrs, [text?, Elements?,...])

Create an HTMLElement. The first argument must be the tag name, you may use a fully qualified tagname for building e.g. XML documents: h('ns:tag').

classes & id

If the tag name is of form name.class1.class2#id that is a shortcut for setting the class and id.

default tag name

If the tag name begins with a class or id, it defaults to a <div>.

Attributes

If an {} object is passed in it will be used to set attributes.

var h = require('hyperscript')
h('a', {href: 'https://npm.im/hyperscript'}, 'hyperscript')

Note that hyperscript sets properties on the DOM element object, not attributes on the HTML element. This makes for better consistency across browsers and a nicer API for booleans. There are some gotchas, however. Attributes such as colspan are camel cased to colSpan, and for on the label element is htmlFor to avoid collision with the language keyword. See the DOM HTML specification for details.

events

If an attribute is a function, then it will be registered as an event listener.

var h = require('hyperscript')
h('a', {href: '#',
  onclick: function (e) {
    alert('you are 1,000,000th visitor!')
    e.preventDefault()
  }
}, 'click here to win a prize')

styles

If an attribute has a style property, then that will be handled specially.

var h = require('hyperscript')
h('h1.fun', {style: {'font-family': 'Comic Sans MS'}}, 'Happy Birthday!')

or as a string

var h = require('hyperscript')
h('h1.fun', {style: 'font-family: Comic Sans MS'}, 'Happy Birthday!')

You may pass in attributes in multiple positions, it's no problem!

children - string

If an argument is a string, a TextNode is created in that position.

children - HTMLElement

If a argument is a Node (or HTMLElement), for example, the return value of a call to h that's cool, too.

children - null.

This is just ignored.

children - Array

Each item in the array is treated like a ordinary child. (string or HTMLElement) this is useful when you want to iterate over an object:

var h = require('hyperscript')
var obj = {
  a: 'Apple',
  b: 'Banana',
  c: 'Cherry',
  d: 'Durian',
  e: 'Elder Berry'
}
h('table',
  h('tr', h('th', 'letter'), h('th', 'fruit')),
  Object.keys(obj).map(function (k) {
    return h('tr',
      h('th', k),
      h('td', obj[k])
    )
  })
)

Cleaning Up

If you need to clean up a widget created using hyperscript - deregistering all its event handlers and observable listeners, you can use context().

var h = require('hyperscript').context()
var o = require('observable')
var text = o()
text('click here to win a prize')
h('a', {href: '#',
  onclick: function (e) {
    text('you are 1,000,000th visitor!')
    e.preventDefault()
  }
}, text)

// then if you want to remove this widget from the page
// to cleanup
h.cleanup()

Ecosystem

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].