npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xus

v0.8.0

Published

logic-less applications

Downloads

49

Readme

xūs

logic-less applications.

wercker status

introduction

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

install

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.

example

given this template:

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

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.

language

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

variables

template:

<h1>{name}</h1>

state:

{
  "name": "Paul Atreides"
}

output:

<h1>Paul Atreides</h1>

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

sections

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

template:

<ul>
  {#fruits}
  <li>
    {name}
    {#vitamins}
      <span>{name}</span>
    {/vitamins}
  </li>
  {/fruits}
</ul>

state:

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

output:

<ul>
  <li>
    Kiwi
    <span>B-6</span>
    <span>C</span>
  </li>
  <li>
    Mango
  </li>
</ul>

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.

template:

<div>
  {^fruits}
    No fruits :(
  {/fruits}
</div>

state:

{
  "fruits": []
}

output:

<div>
  No fruits :(
</div>

comments

Comments begin with a bang and are ignored.

template:

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

will render as follows:

<h1>Today.</h1>

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.

example:

const state = { n: 1 }

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

const optionsWithRegistry = {
    ...options,
    ...{
        registry: {
            foo: foo
        }
    }
}

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

  ReactDOM.render(res, el)
})

internals

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.

typed

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

streams

xūs can handle nodejs streams.

roadmap

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