Comparing version 0.1.5 to 0.1.6
@@ -8,3 +8,3 @@ var xjst = require('../xjst'), | ||
XJSTTranslator = xjst.ometa.XJSTTranslator, | ||
XJSTCompiler = xjst.ometa.XJSTCompiler; | ||
XJSTCompiler = xjst.ometa.XJSTCompiler, | ||
XJSTLocalAndApplyCompiler = xjst.ometa.XJSTLocalAndApplyCompiler; | ||
@@ -446,2 +446,3 @@ | ||
var result; | ||
if (options.wrap) { | ||
@@ -464,6 +465,20 @@ result = ['(function(exports) {'].concat( | ||
// filename is optional argument | ||
if (options === undefined && typeof filename === 'object') { | ||
options = filename; | ||
filename = null; | ||
} | ||
var parsed = exports.parse(code), | ||
compiled = exports.generate(parsed, options); | ||
compiled = exports.generate(parsed, options), | ||
evaluated = vm.runInThisContext(compiled, filename); | ||
return vm.runInThisContext(compiled, filename); | ||
function render(locals) { | ||
return evaluated.apply.call(locals); | ||
}; | ||
render.apply = evaluated.apply; | ||
render.mergeWith = evaluated.mergeWith; | ||
render.config = evaluated.config; | ||
return render; | ||
}; |
{ | ||
"name" : "xjst", | ||
"description" : "XSLT inspired JavaScript templates (with spices)", | ||
"version" : "0.1.5", | ||
"homepage": "http://github.com/veged/xjst", | ||
"author" : "Sergey Berezhnoy <veged@mail.ru> (http://github.com/veged)", | ||
"contributors": [ | ||
"Fedor Indutny <fedor.indutny@gmail.com> (https://github.com/indutny)" | ||
], | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "http://github.com/veged/xjst.git" | ||
}, | ||
"main" : "./lib/xjst", | ||
"directories" : { "lib" : "./lib" }, | ||
"bin" : { "xjst" : "./bin/xjst" }, | ||
"dependencies" : { | ||
"coa": "0.1.x", | ||
"ometajs": "2.0.x", | ||
"uglify-js": "1.1.x" | ||
}, | ||
"devDependencies": { | ||
"benchmark": "0.2.x", | ||
"cliff": "0.1.x", | ||
"q": "0.7.x", | ||
"microtime": "0.1.x", | ||
"watch": "0.3.x", | ||
"nodeunit": "0.5.x" | ||
}, | ||
"engines" : { "node" : ">=0.2.0" }, | ||
"licenses" : [ { "type" : "AS IS" } ], | ||
"scripts": { | ||
"prepublish": "make -B", | ||
"publish": "make clean", | ||
"test": "make test" | ||
"name": "xjst", | ||
"description": "XSLT inspired JavaScript templates (with spices)", | ||
"version": "0.1.6", | ||
"homepage": "http://github.com/veged/xjst", | ||
"author": "Sergey Berezhnoy <veged@mail.ru> (http://github.com/veged)", | ||
"contributors": [ | ||
"Fedor Indutny <fedor.indutny@gmail.com> (https://github.com/indutny)" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/veged/xjst.git" | ||
}, | ||
"main": "./lib/xjst", | ||
"directories": { | ||
"lib": "./lib" | ||
}, | ||
"bin": { | ||
"xjst": "./bin/xjst" | ||
}, | ||
"dependencies": { | ||
"coa": "0.1.x", | ||
"ometajs": "~ 2.0.11", | ||
"uglify-js": "1.1.x" | ||
}, | ||
"devDependencies": { | ||
"benchmark": "0.2.x", | ||
"cliff": "0.1.x", | ||
"q": "0.7.x", | ||
"microtime": "0.1.x", | ||
"watch": "0.3.x", | ||
"nodeunit": "0.5.x" | ||
}, | ||
"engines": { | ||
"node": ">=0.2.0" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "AS IS" | ||
} | ||
} | ||
], | ||
"scripts": { | ||
"prepublish": "make -B", | ||
"publish": "make clean", | ||
"test": "make test" | ||
} | ||
} |
158
README.md
# XJST | ||
XSLT inspired JavaScript templates (with spices). | ||
Built on [NodeJS](http://nodejs.org) and [OMeta/JS](https://github.com/veged/ometa-js). | ||
## What is XJST? | ||
XJST is a performance oriented template engine implemented for [node.js](1). | ||
It's partially inspired by XSLT and built on [ometajs](2). | ||
## Installation | ||
npm install xjst | ||
```bash | ||
npm install xjst | ||
``` | ||
## Examples | ||
## Public API | ||
It is still at an early stage of development. | ||
The easiest and ugly way to look at examples of this: | ||
```javascript | ||
var xjst = require('xjst'); | ||
git clone git://github.com/veged/xjst.git | ||
make -C xjst tests | ||
make -C xjst tests/menu | ||
var fn = xjst.compile('template string', 'filename.xjst', options); | ||
More low-level details are available in `tests/tests.js`. | ||
fn({ your: 'data' }); | ||
``` | ||
You can also use `bin/xjst` to compile `*.xjst` to CommonJS module. | ||
## Syntax | ||
XJST extends javascript syntax with following keywords: `template`, `local`, | ||
`apply`. | ||
### Template | ||
```javascript | ||
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 | ||
```javascript | ||
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: | ||
```javascript | ||
local(this.x = 2, this.y = 3) ... | ||
``` | ||
Use `local` with block: | ||
```javascript | ||
local(...) { var a = 1; return a * 2; } | ||
``` | ||
Or as expression: | ||
```javascript | ||
var newX = local(x = 2) x; | ||
``` | ||
### Apply | ||
```javascript | ||
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 | ||
```bash | ||
$ 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: | ||
```javascript | ||
template(this.type === 'a') { | ||
// body 1 | ||
} | ||
template(this.type === 'b') { | ||
// body 2 | ||
} | ||
``` | ||
Output (simplified): | ||
```javascript | ||
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](https://github.com/veged/xjst/blob/master/doc/tech.ru.md). | ||
Some technical details (in Russian) can be found in [doc/tech.ru.md](https://github.com/veged/xjst/blob/master/doc/tech.ru.md). | ||
#### Authors | ||
[Sergey Berezhnoy](https://github.com/veged), | ||
[Andrey Mischenko](https://github.com/druxa), | ||
[Fedor Indutny](https://github.com/indutny). | ||
#### Links | ||
[1] http://nodejs.org/ | ||
[2] https://github.com/veged/ometa-js |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
990
160
112603
44
Updatedometajs@~ 2.0.11