All Projects → davidjamesstone → hyperviews

davidjamesstone / hyperviews

Licence: other
Template language that produces `h` output

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to hyperviews

hyperapp-tutorial
A tutorial application written in hyperapp
Stars: ✭ 20 (-23.08%)
Mutual labels:  hyperapp
hyperapp-logger
Log Hyperapp state updates and action information to the console.
Stars: ✭ 48 (+84.62%)
Mutual labels:  hyperapp
hyperstart.io
Source code for https://www.hyperstart.io
Stars: ✭ 16 (-38.46%)
Mutual labels:  hyperapp
gender-render
Template-system and proof-of-concept for rendering gender-neutral text-, email- and RPG-text-templates with the correct pronouns of all people involved.
Stars: ✭ 21 (-19.23%)
Mutual labels:  template-language
linkcast
Share links, images, blogs and everything on the web with your friends in one click using this chrome extension Linkcast
Stars: ✭ 13 (-50%)
Mutual labels:  hyperapp
hyperapp-boilerplate
Boilerplate for developing a static web app using Hyperapp built with Parcel
Stars: ✭ 17 (-34.62%)
Mutual labels:  hyperapp
osjs-gui
OS.js GUI Module
Stars: ✭ 19 (-26.92%)
Mutual labels:  hyperapp
hyperapp-todo-parcel
marcusasplund.github.io/hyperapp-todo-parcel/
Stars: ✭ 12 (-53.85%)
Mutual labels:  hyperapp
hyperstatic
Routing, prerendering, code splitting and prefetching for hyperapp
Stars: ✭ 56 (+115.38%)
Mutual labels:  hyperapp
sourcery-templates
Building Vapor projects using meta programming with Sourcery ✨
Stars: ✭ 24 (-7.69%)
Mutual labels:  template-language
hyperapp-styled-components
styled-components for hyperapp in under 3kb
Stars: ✭ 17 (-34.62%)
Mutual labels:  hyperapp
rocket-emoji
🚀 Find and copy an emoji to your clipboard in one click.
Stars: ✭ 42 (+61.54%)
Mutual labels:  hyperapp
hyper-broadcast
Extension for Hyper.app to broadcast user inputs to multiple terms.
Stars: ✭ 15 (-42.31%)
Mutual labels:  hyperapp
re-hyperapp
Almost zero-cost bindings for the https://github.com/hyperapp/hyperapp UI library.
Stars: ✭ 21 (-19.23%)
Mutual labels:  hyperapp
create-hyperapp
Create Hyperapps with no build configuration
Stars: ✭ 16 (-38.46%)
Mutual labels:  hyperapp
hyperlight
Next-gen Hyperapp framework.
Stars: ✭ 22 (-15.38%)
Mutual labels:  hyperapp
Splain
small parser to create more interesting language/sentences
Stars: ✭ 15 (-42.31%)
Mutual labels:  template-language
closet
The Web Framework for Flashcards
Stars: ✭ 45 (+73.08%)
Mutual labels:  template-language
khufu
A template language for incremental-dom or DSL for javascript views
Stars: ✭ 19 (-26.92%)
Mutual labels:  template-language
karkas
A tiny template engine based on TypeScript
Stars: ✭ 14 (-46.15%)
Mutual labels:  template-language

hyperviews

hyperviews is a template language that transforms to hyperscript.

Use it as a build tool with any h(tag, props, children) compliant framework e.g. React, preact or hyperapp.

const hv = require('hyperviews')

hv("<div id='foo'>{state.name}</div>")
// => h('div', { id: 'foo' }, (state.name))

Installation

npm i hyperviews

API

hyperviews(tmpl, mode, name, argstr)

  • tmpl (required) - The template string.
  • mode - The output format. Can be one of [raw, esm, cjs, browser], or if any other value is passed the function is exported as a variable with that name. The default is raw.
  • name - The default output function name. The default is view.
  • args - The default function arguments. The default is props state.

CLI

Reads the template from stdin,

cat examples/test.html | hyperviews --mode esm --name foo --args bar > examples/test.js

See more CLI examples

Template language

Interpolation

Use curly braces in attributes and text.

<div>
  <a class={state.class} href='http://www.google.co.uk?q={state.query}'></a>
  My name is {state.name} my age is {state.age} and I live at {state.address}
</div>

See more interpolation examples

Conditionals

There are two forms of conditional.

Using an if attribute.

<span if='state.bar === 1'>Show Me!</span>

Or using tags <if>, <elseif> and <else>

<div>
  <if condition='state.bar === 1'>
    <span>1</span>
  <elseif condition='state.bar === 2'>
    <span>2</span>
  <else>
    <span>bar is neither 1 or 2, it's {state.bar}!</span>
  </if>
</div>

if tags can be nested.

See more conditional examples

Iteration

The each attribute can be used to repeat over items in an Array. Three additional variables are available during each iteration: $value, $index and $target.

It supports keyed elements as shown here.

<ul>
  <li each='post in state.posts' key={post.id}>
    <span>{post.title} {$index}</span>
  </li>
</ul>

produces

h('ul', {}, (state.posts || []).map(function ($value, $index, $target) {
  const post = $value
  return h('li', { key: (post.id) }, h('span', {}, (post.title) + ' ' + ($index)))
}, this))

See more iteration examples

Events

<a href='http://example.com' onclick=this.onClick>{state.foo}</a>

produces this output

h('a', { href: 'http://example.com', onclick: this.onClick, (state.foo))

See more event examples

Style

The style attribute expects an object

<p style="{ color: state.color, fontSize: '12px' }"></p>

produces this output

h('p', { style: { color: state.color, fontSize: '12px' } })

Literal

The script tag literally outputs it's contents.

<script>
  import { h, Component } from 'preact'
  import MyComponent from './component.js'
</script>

This is also useful for recursive nodes, e.g. a tree

<if condition=state.children>
  <div>
    <a href='#{state.path}'>{state.name}</a>
    <ul>
      <li each='child in state.children'>
        <script>view(props, child)</script>
      </li>
    </ul>
  </div>
<else>
  <a href='#{state.path}'>{state.name}</a>
</if>

produces this output

function view (props, state) {
  return (function () {
    if (state.children) {
      return h('div', {}, [
        h('a', { href: '#' + (state.path) }, (state.name)),
        h('ul', {}, (state.children || []).map(function ($value, $index, $target) {
          var child = $value
          return h('li', {}, view(props, child))
        }))
      ])
    } else {
      return h('a', { href: '#' + (state.path) }, (state.name))
    }
  })()
}

See more literal examples

Function

The function tag outputs a function, returning it's contents. Supports name and args attributes.

<function name='MyComponent' args='x y z'>
  <div>{x}</div>
</script>

produces this output

function MyComponent (x, y, z) {
  return h('div', null, (x))
}

Components

Components are declared with if the tag starts with a capital letter.

<div>
  <MyComponent foo='bar' />
</div>

produces this output

h('div', null, h(MyComponent, { foo: 'bar' }))

Module example

How you structure your app is down to you. I like to keep js and html in separate files so a component might look like this:

  • MyComponent
    • view.html (The template file e.g. <div>{state.name}</div>)
    • view.html.js (The transformed h output of the file above)
    • index.js (Imports the transformed view and exports the component)

but if you want you could build entire modules in a html file like this:

<script>

  import { h, Component } from 'preact'

  export default class MyComponent extends Component {
    constructor (props) {
      super(props)
      this.render = view

      this.onSubmit = e => {
        e.preventDefault()
        // ...
      }
    }
  }

</script>

<function>
  <section>
    <form onsubmit=this.onSubmit>
      <input type=text name=text value={state.text} />
      <input type=text name=description value={state.description} />
    </form>
  </section>        
</function>

Compiles to

import { h, Component } from 'preact'

export default class MyComponent extends Component {
  constructor (props) {
    super(props)
    this.render = view

    this.onSubmit = e => {
      e.preventDefault()
    // ...
    }
  }
}

function view (props, state) {
  return h('section', null, h('form', { onsubmit: this.onSubmit }, [
    h('input', { type: 'text', name: 'text', value: (state.text) }),
    h('input', { type: 'text', name: 'description', value: (state.description) })
  ]))
}

More examples here

Using browserify? Then install the hyperviewify transform so you can simply require templates.

const view = require('./my-view.html')

npm i hyperviewify

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