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

@mpxjs/template-engine

v2.8.7

Published

template-engine for mpx runtime render

Downloads

1,616

Readme

Template-engine

小程序运行时渲染基础模板生成引擎。

Fork from @tarojs/shared for generating template.

Install

npm install @mpxjs/template-engine
# or
yarn add @mpxjs/template-engine
# or
pnpm add @mpxjs/template-engine

Basic Usage

  1. 生成模板代码
const { createTemplateEngine } = require('@mpxjs/template-engine')

const templateEngine = createTemplateEngine('wx') // 'wx' | 'ali' | 'swan' | 'qq' | 'tt' | 'dd' | 'web' | 'tenon'

const templateCode = templateEngine.buildTemplate({
  baseComponents: ['view', 'button'] // 需要使用的小程序基础组件
})

将会输出

<template name="mpx_tmpl">
  <element r="{{r}}" wx:if="{{r}}" />
</template>
<template name="t_0_view">
  <view data-mpxuid="{{i.d.uid}}">
    <block wx:for="{{i.c}}" wx:key="index">
      <template is="t_1_container" data="{{i:item}}" />
    </block>
  </view>
</template>
<template name="t_0_button">
  <button data-mpxuid="{{i.d.uid}}">
    <block wx:for="{{i.c}}" wx:key="index">
      <template is="t_1_container" data="{{i:item}}" />
    </block>
  </button>
</template>
<template name="t_0_#text" data="{{i:i}}">
  <block>{{i.ct}}</block>
</template>
<template name="t_0_container">
  <template is="t_0_{{i.nt}}" data="{{i:i}}" />
</template>
<template name="t_1_container">
  <block wx:if="{{i.nt === '#text'}}">
    <template is="t_0_#text" data="{{i:i}}" />
  </block>
  <block wx:else>
    <element r="{{i}}" />
  </block>
</template>
  1. 写入文件
const fs = require('fs')
fs.writeFile('base.wxml', code)
  1. 引入基础模板,使用vnode渲染页面
<import src="base.wxml" />
<template is="t_0_container" data="{{ i: r }}" />
Component({
  data: {
    r: {
      // 描述节点的vnode
      nt: 'view', // 节点类型
      d: {
        class: "view-class" // 节点属性值
      },
      c: [ // 子节点
        {
          nt: 'button',
          d: {},
          c: [
            {
              nt: '#text',
              ct: '运行时渲染'
            }
          ]
        }
      ]
    }
  }
})
  1. 模板引擎将会在渲染出正确的组件

Build Options

自定义组件/运行时组件

除了基础组件外,模板引擎还支持自定义组件的渲染

const templateCode = templateEngine.buildTemplate({
  normalComponents: [ 'custom-dialog' , 'custom-popup'], // 需要使用的自定义组件
})

自定义属性名

如果需要自定义某个模板组件的属性,例如生成的组件需要一个class属性。可以通过传入对象配置。

const templateCode = templateEngine.buildTemplate({
  baseComponents: [
    {
      view: ['class']
    }
  ] // 需要使用的小程序基础组件
})

自定义属性值

如果想自定义classdata的取值。

const templateCode = templateEngine.buildTemplate({
  baseComponents: [
    {
      view: {
        class: 'c'
      }
    }
  ] // 需要使用的小程序基础组件
})