logic-less applications
Last updated a year ago by tetsuo .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install xus 
SYNC missed versions from official npm registry.


logic-less applications.

wercker status


xūs is the name of the simple templating languge, it's parser and the runtime library.

It brings the simplicity of mustache and the expressiveness of mobx-state-tree together, and makes laying out reactive user interfaces incredibly easy.

In combination with React and mobx, xūs can compile itself into an observer component tree, allowing it to get notified of the changes on the state and re-render the tree when it's necessary, as how it was structured in xūs in the first place.

A state can be a simple observable value, or a more sophisticated state definition like a mobx-state-tree.

getting started

An in-depth getting started guide can be found here: "Introducing xūs: A reactive template engine on top of mobx"

If you are curious why I’ve started this project and how this could play a role within a larger framework, take a look at "Templates, state trees, a school of ‘whatever’ and a state definition language".


With npm, or yarn, do:

npm install xus

You can also install the UMD bundle into the HTML with <script> tag:

<script src="//unpkg.com/xus/dist/xus.js"></script>

However, the prefered method is using browserify with the xusify transform which gives you pre-compiled render functions when you require() html files. For more information and examples, see xusify.


given this template:

  <p>You have completed <b>{completedCount}</b> of your tasks.</p>
      <li class="{#done}finished{/done}" onClick="toggle">{title}</li>

and this state definition:

import { types } from "mobx-state-tree"

const Todo = types.model("Todo", {
    title: types.string,
    done: false
}, {
    toggle() {
        this.done = !this.done

const State = types.model("State", {
    todos: types.array(Todo),
    get completedCount() {
        return this.todos.reduce((count, todo) => (todo.done ? count + 1 : count), 0)

create a new state:

const state = State.create({
  todos: [
    { title: "Get coffee", done: false },
    { title: "Wake up", done: true }

then produce a ReactElement with xus and render it using ReactDOM:

var options = {
  createElement: React.createElement,
  observer: mobxReact.observer

xus(template, state, options, function(er, newElement) {
  if (er) throw er

  ReactDOM.render(newElement, el)

See the full example here.

See also "Introducing xūs: A reactive template engine on top of mobx" for a detailed explanation of this example.


xūs supports mustache spec with the exception of lambdas and partials.





  "name": "Paul Atreides"


<h1>Paul Atreides</h1>

Note that, xūs does not recursively check the parent contexts if name key is not found.


A section property can be an object, a boolean or an array that will be iterated.




  "fruits": [
      "name": "Kiwi",
      "vitamins": [ { "name": "B-6" }, { "name": "C" } ]
      "name": "Mango"



inverted sections

Inverted sections may render text once based on the inverse value of the key. That is, they will be rendered if the key doesn't exist, is false, or is an empty list.


    No fruits :(


  "fruits": []


  No fruits :(


Comments begin with a bang and are ignored.


<h1>Today{! ignore me }.</h1>

will render as follows:


Comments may contain newlines.

registry mechanism

You can pass in your own component factories in options.registry.

By default, xūs will assume your HTML tags are normal HTML tags unless they resolve to something else in the registry you provided.


const state = { n: 1 }

function foo(props) {
    return <p className={ props.className }>
        { props.children }

const optionsWithRegistry = {
        registry: {
            foo: foo

xus("<foo class=bla><div>{n}</div></foo>", state, optionsWithRegistry, (err, res) => {
  if (err) throw err

  ReactDOM.render(res, el)


import * as xus from "xus"

The main function that is exposed is called xus, and it's only good for creating React/mobx trees.

However, xūs does not ship with React, mobx-react and/or mobx-state-tree, and it can be rendered into a virtual tree (or just tree) of any kind, not necessarily into an observer component tree.

The following example shows how you can build a virtual-dom tree instead of React:

import { Template } from "xus/runtime"
const createElement = require("virtual-dom/create-element")

const tree = render.call(new Template, { n: 1, m: 2 })

// ... you should also move props.attributes to attributes in here, but you get the idea :)

const rootNode = createElement(tree)

xūs Templates are mainly just straight-forward render(state, options) methods and they can be extended very easily.

See the API Reference for more information.


Installing xūs with npm brings with it type definitions for TypeScript as well.


xūs can handle nodejs streams.


See the Issues page for a list of known bugs and planned features.

Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 23
Dependencies (6)
Dev Dependencies (25)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |