微信小程序-利用wxParse将html转为wxml

1、到https://github.com/icindy/wxParse下载

2.1 在使用的View中引入WxParse模块

var WxParse = require('../../wxParse/wxParse.js');
  • 1

2.2 在使用的Wxss中引入WxParse.css,可以在app.wxss

@import "/wxParse/wxParse.wxss";
  • 1

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4、模版引用

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

目录结构如下:

这里写图片描述

demo.wxml

<import src="../../wxParse/wxParse.wxml"/> 
<view class="wxParse">
    <template is="wxParse" data="{{wxParseData:article.nodes}}"/>
</view>

<view style="padding: 20px 10px; background-color:#eee;">
    <block wx:for="{{replyTemArray}}" wx:key="">
        回复{{index}}:<template is="wxParse" data="{{wxParseData:item}}"/>
    </block>
</view>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

demo.js

var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
  },
  onLoad: function () {
    var that = this;
    /**
     * 初始化emoji设置
     */
    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",
    });
    /**
     * html解析示例
     */
    var article = `
      <div style="text-align:center;margin-top:10px;">
        <img src="https://weappdev.com/uploads/default/original/1X/84512e0f4591bcf0dee42c3293f826e0c24b560c.jpg" alt="wxParse-微信小程序富文本解析组件Logo">    
        <h1 style="color:red;">wxParse-微信小程序富文本解析组件</h1>
        <h2>支持Html及markdown转wxml可视化</h2>
    </div>

    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持的标签</h3>
        <blockquote>wxParse支持70%的html的标签</blockquote>
        <div style="margin-top:10px;">
            <span>span标签</span>
            <strong>strong标签</strong>
        </div>
    </div>

    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持内联样式style</h3>
        <blockquote>wxParse可以渲染原html带有的style样式</blockquote>
        <div style="margin-top:10px;">
            <span>span标签</span>
            <strong>strong标签</strong>
        </div>
    </div>

    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持class潜入</h3>
        <blockquote>wxParse可以注入html带有的class属性</blockquote>
    </div>

    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持emojis小表情</h3>
        <blockquote>wxParse可以解析固定格式的小表情标签</blockquote>
        <div style="margin-top:10px;">
            <p>这里可以解析出emoji的表情[00][01][02][03][04][05][06][07][08][09]</p>
        </div>
    </div>

    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持图片自适应</h3>
        <blockquote>wxParse可以动态计算图片大小并进行自适应适配</blockquote>
        <div style="margin-top:10px;">
            <img src="http://a.hiphotos.baidu.com/image/pic/item/9a504fc2d5628535959cf4cf94ef76c6a6ef63db.jpg" alt="">
            <img src="http://e.hiphotos.baidu.com/image/pic/item/48540923dd54564e1e1ac2d7b7de9c82d0584fe4.jpg" alt="">
        </div>
    </div>

    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持图片点击预览,左右滑动预览</h3>
        <blockquote>wxParse可以讲一篇文章中的几个图片一起预览</blockquote>
        <div style="margin-top:10px;">
            你可以点击上面的图片,将会进入预览视图,同时左右滑动可以切换图片预览
        </div>
    </div>

    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持多数据循环渲染</h3>
        <blockquote>wxParse支持特定设置下的多数据渲染,适用于在评论、多文本分别渲染等</blockquote>
        <div style="margin-top:10px; text-align:center;">
            请继续向下看,循环渲染多条html评论
        </div>
    </div>
    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持短table标签</h3>
        <blockquote>wxParse目前对于table的支持比较有限</blockquote>
        <div style="margin-top:10px; text-align:center;">
            <table>
      <tr>
            <th>标题1</th>
            <th>标题2</th>
            <th>标题3</th>
            <th>标题4</th>
            <th>标题5</th>
        </tr>
      <tr>
          <td>cell1</td>
            <td>cell2</td>
            <td>cell3</td>
            <td>cell4</td>
            <td>cell5</td>
        </tr>
        <tr>
          <td>cell1[03]</td>
            <td>cell2</td>
            <td>cell3</td>
            <td>cell4</td>
            <td>cell5</td>
        </tr>
    </table>
        </div>
    </div>
    `;

    WxParse.wxParse('article', 'html', article, that,5);


    /**
     * 多数据解析示例
     */
    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)
      }
    }
  }


})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184

app.wxss

@import "/wxParse/wxParse.wxss";
  • 1

效果图: 
这里写图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章