Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xjst

Package Overview
Dependencies
Maintainers
2
Versions
162
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xjst

XSLT inspired JavaScript templates (with spices)

  • 0.1.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
113
decreased by-57.84%
Maintainers
2
Weekly downloads
 
Created
Source

XJST

What is XJST?

XJST is a performance oriented template engine implemented for node.js. It's partially inspired by XSLT and built on ometajs.

Installation

npm install xjst

Public API

var xjst = require('xjst');

var fn = xjst.compile('template string', 'filename.xjst', options);

fn({ your: 'data' });

Syntax

XJST extends javascript syntax with following keywords: template, local, apply.

Template

template(expression1 === value1 && ... && expressionN === valueN) {
  // will be run if condition above equals to true
}

Multiple template statements will be grouped to construct optimal conditions graph. Order of template statements matters, priority decreases from bottom to top.

Local

var x = 1;

console.log(local(x = 2) x); // 2
console.log(x); // 1

local allow you to make temporary changes to visible variable scope. Every assignment put inside parens will be reverted immediately after expression execution.

You can make multiple assignments:

local(this.x = 2, this.y = 3) ...

Use local with block:

local(...) { var a = 1; return a * 2; }

Or as expression:

var newX = local(x = 2) x;

Apply

template(true) {
  return apply(this.type = 'first');
}

template(this.type === 'first') {
  return apply({ type: 'second' });
}

template(this.type === 'second') {
  return 'here am I';
}

XJST is intended to be applied recursively to the same data, while making small reversible changes to it. apply keyword works exactly like local (applying changes in parens and reverting them after execution), but with small distinction - apply statement doesn't have a body, so it's just doing some changes to date and applying template to changed data (context will be preserved).

CLI interface

$ bin/xjst --help

Usage:
  xjst [OPTIONS] [ARGS]


Options:
  -h, --help : Help
  -i INPUT, --input=INPUT : Input file (default: stdin)
  -o OUTPUT, --output=OUTPUT : Output file (default: stdout)

$ bin/xjst -i template.xjst

.... some code ...

Optimizations

XJST takes all template statements and produces a tree with comparisons in nodes and template's bodies in leafs. apply are handled and replaced by direct calls to tree's nodes (some of comparisons can be skipped, using context's state).

Input:

template(this.type === 'a') {
  // body 1
}
template(this.type === 'b') {
  // body 2
}

Output (simplified):

switch (this.type) {
  case 'a':
    // body 1
    break;
  case 'b':
    // body 2
    break;
}

Documentation

Some technical details (in Russian) can be found in doc/tech.ru.md.

Authors

Sergey Berezhnoy, Andrey Mischenko, Fedor Indutny.

[1] http://nodejs.org/

[2] https://github.com/veged/ometa-js

FAQs

Package last updated on 03 Nov 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc