$ cnpm install weex-templater
<template> Transformer<template> element to JSON-like objectparse(code, done)validate(json, done)/**
* Parse `<template>` code to a JSON-like Object and log errors & warnings
*
* @param {string} code
* @param {function} done
*/
function parse(code, done) {}
/**
* Validate a JSON-like Object and log errors & warnings
*
* @param {object} json
* @param {function} done
*/
function validate(json, done) {}
/**
* Result callback
*
* data
* - jsonTemplate{type, attr, style, events, shown, repeat}
* - deps[tagname]
* - log[{line, column, reason}]
*
* @param {Error} err
* @param {object} data
*/
function done(err, data) {}
k: {{v}}; ... (styler.validate needed)a {{v}} cvar templater = require('weex-templater')
var code = '<template><container><text>Hello</text><img class="a {{x}} c" src="{{y}}" /><image style="opacity: {{z}}"></image></container></template>'
templater.parse(code, function (err, data) {
// syntax error
// format: {line, column, reason, ...}
err
// result
// {
// type: 'container',
// children: [
// {
// type: 'text',
// value: 'Hello'
// },
// {
// type: 'img',
// class: function () {return ['a', this.x, 'c']},
// attr: {
// src: function () {return this.y}
// }
// },
// {
// type: 'img',
// style: {
// opacity: function () {return this.z}
// }
// }
// ]
// }
data.jsonTemplate
// ['container', 'text', 'img']
data.deps[]
// format: {line, column, reason}
// - Error: `image` tag should have a `src` attr
// - NOTE: autofixed `image` tag name to `img`
data.log[]
})
var json = {
type: 'container',
children: [
{
type: 'text',
value: 'Hello'
},
{
type: 'image',
class: 'a {{x}} c',
attr: {
src: '{{y}}'
}
},
{
type: 'img',
style: {
opacity: '{{z}}'
}
}
]
}
Copyright 2013 - present © cnpmjs.org | Home |