Home

Awesome

Mark Template

Mark Template is a new JS template engine inspired by React JSX and XSLT. It's template syntax is based on Mark, a new markup notation that has unified support of JSON, HTML and XML.

Features of Mark Template:

Template Reference

1. Template root

2. Top-level elements

Like React JSX and XSLT, each Mark Template script is actually a collection of transformation templates and functions. The top-level elements that can directly appear under the {template} root are:

3. Component Transformation

The following section describes the element that can nest inside the component/function body, including pre-defined elements that carry out specific transformation logic, and literal elements. Elements other than these pre-defined ones are treated as literal elements. Transformation elements and literal elements can nest within each other.

3.1 Processing of element content

The contents a template element are processed in the following prioritized way:

3.2 Pre-defined transformation elements

3.3 Component and function invocation

3.4 Inline expressions

Elements whose name start with this and Mark pragmas are treated as inline JS expressions.

The result of evaluating the expression is passed to the template engine output adaptor. It may be casted into string or preserved as value, depending on the output adaptor.

3.5 Literal elements

4. Output Adaptors

Mark Template can support wide-range of output, through built-in and custom adaptors. The built-in adaptors can support Mark, JSON, HTML, XML, Text, DOM, virtual-dom.

Usage

Install from NPM:

npm install mark mark-template --save

Then in your node script, use it as:

const Mark = require('mark');
const Template = require('mark-template');
var tmpl = Template.compile(`... template source ...`);
var model = Mark.parse(`... model data ...`);
var output = Template.apply(tmpl, model);
console.log("Output: " + Mark.stringify(output));

To use the library in browser, you can include the mark-template.js under /dist directory into your html page, like:

<script src='mark.js'></script>
<script src='mark-template.js'></script>
<script> 
var tmpl = MarkTemplate.compile(`... template source ...`);
var model = Mark.parse(`... model data ...`);
var output = MarkTemplate.apply(tmpl, model);
console.log("Output: " + Mark.stringify(output));
</script>