1. weex-templater
Weex <template> transformer
weex-templater
Package: weex-templater
Created by: weexteam
Last modified: Wed, 29 Jun 2022 00:52:52 GMT
Version: 0.3.5
License: GPL-3.0
Downloads: 405
Repository: https://github.com/weexteam/weex-templater

Install

npm install weex-templater
yarn add weex-templater

Weex <template> Transformer

NPM version
Build status
Downloads

Features

  • convert a <template> element to JSON-like object
  • autofix common mistakes & friendly warnings
    • tag name
    • attr
    • text content
  • parse data binding and expressions
  • make sure only one root node

API

  • parse(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) {}

Validation

  • root check: only one root element (ignore others)
  • tagname check: native or hyphenated (autofix or warn non-hyphenated custom tagname)
  • child/parent check: elements required certain child/parent (error)
  • attr value check: special tag[attr] value (error or autofix or warn)
  • text content check: only text element allows text content (error)
  • data binding check:
    • normal value (exp converting)
    • event: non-mustache -> mustache (autofix)
    • style: k: {{v}}; ... (styler.validate needed)
    • class: a {{v}} c

Demo

 var 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}}'
      }
    }
  ]
}

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code