superviews.js
On the server superviews.js
is used as a template engine for google's incremental-dom.
It can also now be used in the browser to help build web applications based on Custom Elements V1
Try it out live in your browser
npm install superviews.js --save
API
tmplstr
(required) - The template string.
name
- The output function name (will be overridden with a <template>
element).
argstr
- The output function arguments (will be overridden with a <template>
element).
mode
- The output format. Can be one of ['es6', 'cjs', 'browser', 'amd'], if any other value is passed the function is exported as a variable with that name.
superviews(tmplstr, name, argstr, mode)
CLI
cat examples/test.html | superviews --mode=es6 --name=foo --argstr=bar > examples/test.js
Client
NEW! superviews can now be used in the browser.
Use it as a clientside library with a set of helpful classes and methods for building web applications based on the Web Components spec, specifically Custom Elements V1.
Example
Create a file called tmpl.html
<!--If the outermost element is a `template` element and containsan `args` attribute it will be used as the function definition.A `name` attribute can also be supplied. These will be used todefine the enclosing function name and arguments in the incremental-dom output (see below).--> <!-- `script` tags that have no attributes are treated as literal javascript and will be simply inlined into the incremental-dom output. --> <!-- Attribute values can be set using javascript between curly braces {} --> <!-- Attributes are omitted if their expression is null or undefined. Useful for `checked`, `disabled` --> <!-- Interpolation in attributes --> <!-- Text Interpolation --> My name is {data.name} my age is {data.age} I live at {data.address} <!-- Any javascript can be used --> Hover for json <!-- 'on' event handlers. $event and $element are available to use in the handler. --> Say hi Some link <!-- Use an `if` attribute for conditional rendering --> description <!-- An `if` tag can also be used for conditional rendering by adding a `condition` attribute. --> I'm in an `if` block. <!-- `elseif` and `else` tags can also be used --> 1 2 Default <!-- Use a `skip` attribute for conditional patching of children --> <!-- The `style` attribute is special and can be set with an object. --> My style changes <!-- The `each` attribute can be used to repeat over items. This includes iterating over keys on an Object or any object that has a forEach function e.g. an Array, Map, Set. Three variables are available for each iteration: $value, $item and $target.--> {$item} <!-- Looping over arrays --> {item.name} <!-- Looping over object keys --> {key} - {data.obj[key]} <!-- The `each` attribute also supports defining a `key` to use. For Arrays and Objects this is done automatically for you. If you are iterating a Map, this should be set to identify each item in the list. This allow the diff patch in to keep track of each item in the list. See http://google.github.io/incremental-dom/#conditional-rendering/array-of-items. The key used here is `product.id`. --> {product.name} <!-- Conditional iteration --> {item.name} No items found
cat tmpl.html | superviews > tmpl.js
Converts the template above to this incremental-dom code:
; {var hoisted1 = "type" "text"var hoisted2 = "type" "text"var hoisted3 = "href" "#"var hoisted4 = "title" "hello"var hoisted5 = "class" "list-header"var __target return { { todos } { todos } if datashowMe if datashowMe if datafoo === 1 else if datafoo === 2 else if dataskipMe else __target = dataitems if __target ;__targetforEach ? __target : Object __target = dataarr if __target ;__targetforEach ? __target : Object __target = dataobj if __target ;__targetforEach ? __target : Object __target = dataproducts if __target ;__targetforEach ? __target : Object if dataitemslength __target = dataarr if __target ;__targetforEach ? __target : Object if !dataitemslength }}
browserify
Using browserify? There's the superviewify transform allowing you to simply require your templates and have them automatically compiled to incremental-dom javascript.
npm install superviewify --save
License
MIT