微信小程序输出html内容数据插件wxParse

来源:互联网 发布:java sleep使用 编辑:程序博客网 时间:2024/06/07 03:46

微信小程序输出html内容数据插件wxParse,可以把带html标签的数据输出为微信小程序正常显示的格式,测试可用,不过外观样式,可能需要根据你的需求再调整一下,wxParse插件带有演示,也有使用文档说明。
下载地址:https://github.com/icindy/wxParse

 wxParse文档基本使用方法

1. Copy文件夹wxParse

- wxParse/  -wxParse.js(必须存在)  -html2json.js(必须存在)  -htmlparser.js(必须存在)  -showdown.js(必须存在)  -wxDiscode.js(必须存在)  -wxParse.wxml(必须存在)  -wxParse.wxss(必须存在)  -emojis(可选)

2. 引入必要文件

//在使用的View中引入WxParse模块var WxParse = require('../../wxParse/wxParse.js');
//在使用的Wxss中引入WxParse.css,可以在app.wxss@import "/wxParse/wxParse.wxss";

3. 数据绑定

var article = '<div>我是HTML代码</div>';/*** WxParse.wxParse(bindName , type, data, target,imagePadding)* 1.bindName绑定的数据名(必填)* 2.type可以为html或者md(必填)* 3.data为传入的具体数据(必填)* 4.target为Page对象,一般为this(必填)* 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)*/var that = this;WxParse.wxParse('article', 'html', article, that,5);

4. 模版引用

//这里data中article为bindName<template is="wxParse" data="{{wxParseData:article.nodes}}"/>

 高级用法

配置小表情emojis

/*** WxParse.emojisInit(reg,baseSrc,emojis)* 1.reg,如格式为[00]=>赋值 reg='[]'* 2.baseSrc,为存储emojis的图片文件夹* 3.emojis,定义表情键值对*/WxParse.emojisInit('[]', "/wxParse/emojis/", {      "00": "00.gif",      "01": "01.gif",      "02": "02.gif",      "03": "03.gif",      "04": "04.gif",      "05": "05.gif",      "06": "06.gif",      "07": "07.gif",      "08": "08.gif",      "09": "09.gif",      "09": "09.gif",      "10": "10.gif",      "11": "11.gif",      "12": "12.gif",      "13": "13.gif",      "14": "14.gif",      "15": "15.gif",      "16": "16.gif",      "17": "17.gif",      "18": "18.gif",      "19": "19.gif",    });

wxParse多数据循环使用方法

介绍如何使用wxParse在回复等多条HTML共同渲染的方法

 方法介绍

/*** WxParse.wxParseTemArray(temArrayName,bindNameReg,total,that)* 1.temArrayName: 为你调用时的数组名称* 3.bindNameReg为循环的共同体 如绑定为reply1,reply2...则bindNameReg = 'reply'* 3.total为reply的个数* 懒人建站http://www.51xuediannao.com/        *  var that = this; WxParse.wxParseTemArray("replyTemArray",'reply', replyArr.length, that)*/

使用方式

循环绑定数据

var replyHtml0 = `<div style="margin-top:10px;height:50px;">        <p class="reply">            wxParse回复0:不错,喜欢[03][04]        </p>        </div>`;    var replyHtml1 = `<div style="margin-top:10px;height:50px;">        <p class="reply">            wxParse回复1:不错,喜欢[03][04]        </p>        </div>`;    var replyHtml2 = `<div style="margin-top:10px;height:50px;">        <p class="reply">            wxParse回复2:不错,喜欢[05][07]        </p>        </div>`;    var replyHtml3 = `<div style="margin-top:10px;height:50px;">        <p class="reply">            wxParse回复3:不错,喜欢[06][08]        </p>        </div>`;    var replyHtml4 = `<div style="margin-top:10px; height:50px;">        <p class="reply">            wxParse回复4:不错,喜欢[09][08]        </p>        </div>`;    var replyHtml5 = `<div style="margin-top:10px;height:50px;">        <p class="reply">            wxParse回复5:不错,喜欢[07][08]        </p>        </div>`;    var replyArr = [];    replyArr.push(replyHtml0);    replyArr.push(replyHtml1);    replyArr.push(replyHtml2);    replyArr.push(replyHtml3);    replyArr.push(replyHtml4);    replyArr.push(replyHtml5);    for (let i = 0; i < replyArr.length; i++) {      WxParse.wxParse('reply' + i, 'html', replyArr[i], that);      if (i === replyArr.length - 1) {        WxParse.wxParseTemArray("replyTemArray",'reply', replyArr.length, that)      }    }

模版使用

<block wx:for="{{replyTemArray}}" wx:key="">        回复{{index}}:<template is="wxParse" data="{{wxParseData:item}}"/>    </block>

下载地址:https://github.com/icindy/wxParse

阅读全文
0 0