今天看到一个github项目 wxParse , 功能是将json描述的富文本解析为wxml结构。看到源码的时候我笑了,为什么?因为解析的代码实在太愚蠢了,但这和模板中不能引用自身有关。希望这点能改进吧。
看代码,注意这里定义了wxParse0到wxParse11共12个模板,这12个模板代码除了解析子结构调用不同的wxParseXXX模板之外其余代码都是一样的。为什么?因为模板不能自引用啊。假如在wxParse0中解析子结构时能调用自身(wxParse0),那就不需要再定义剩余的11个模板了。更为重要的是,这种处理方式其实是处理不了dom树超过12层的,更深层次的子结构就解析不了了,但由于小程序的机制,这样也不会报错,导致查bug很困难。
限于篇幅,不能将12个模板一起贴出,以省略号代替。
这是我的一点小小的意见,希望官方开发人员能重视。
<!--** * author: Di (微信小程序开发工程师) * organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com) * 垂直微信小程序开发交流社区 * * github地址: https://github.com/icindy/wxParse * * for: 微信小程序富文本解析 * detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184 */--> <!--基础元素--> < template name = "wxParseVideo" > <!--增加video标签支持,并循环添加--> < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < video class = "{{item.classStr}} wxParse-{{item.tag}}-video" src = "{{item.attr.src}}" ></ video > </ view > </ template > < template name = "wxParseImg" > < image class = "{{item.classStr}} wxParse-{{item.tag}}" data-from = "{{item.from}}" data-src = "{{item.attr.src}}" data-idx = "{{item.imgIndex}}" src = "{{item.attr.src}}" mode = "aspectFit" bindload = "wxParseImgLoad" bindtap = "wxParseImgTap" mode = "widthFix" style = "width:{{item.width}}px;" /> </ template > < template name = "WxEmojiView" > < view class = "WxEmojiView wxParse-inline" style = "{{item.styleStr}}" > < block wx:for = "{{item.textArray}}" wx:key = "" > < block class = "{{item.text == '\\n' ? 'wxParse-hide':''}}" wx:if = "{{item.node == 'text'}}" >{{item.text}}</ block > < block wx:elif = "{{item.node == 'element'}}" > < image class = "wxEmoji" src = "{{item.baseSrc}}{{item.text}}" /> </ block > </ block > </ view > </ template > < template name = "WxParseBr" > < text >\n</ text > </ template > <!--入口模版--> < template name = "wxParse" > < block wx:for = "{{wxParseData}}" wx:key = "" > < template is = "wxParse0" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse0" > <!--<template is="wxParse1" data="{{item}}" />--> <!--判断是否是标签节点--> < block wx:if = "{{item.node == 'element'}}" > < block wx:if = "{{item.tag == 'button'}}" > < button type = "default" size = "mini" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse1" data = "{{item}}" /> </ block > </ button > </ block > <!--li类型--> < block wx:elif = "{{item.tag == 'li'}}" > < view class = "{{item.classStr}} wxParse-li" style = "{{item.styleStr}}" > < view class = "{{item.classStr}} wxParse-li-inner" > < view class = "{{item.classStr}} wxParse-li-text" > < view class = "{{item.classStr}} wxParse-li-circle" ></ view > </ view > < view class = "{{item.classStr}} wxParse-li-text" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse1" data = "{{item}}" /> </ block > </ view > </ view > </ view > </ block > <!--video类型--> < block wx:elif = "{{item.tag == 'video'}}" > < template is = "wxParseVideo" data = "{{item}}" /> </ block > <!--img类型--> < block wx:elif = "{{item.tag == 'img'}}" > < template is = "wxParseImg" data = "{{item}}" /> </ block > <!--a类型--> < block wx:elif = "{{item.tag == 'a'}}" > < view bindtap = "wxParseTagATap" class = "wxParse-inline {{item.classStr}} wxParse-{{item.tag}}" data-src = "{{item.attr.href}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse1" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'table'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse1" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'br'}}" > < template is = "WxParseBr" ></ template > </ block > <!--其他块级标签--> < block wx:elif = "{{item.tagType == 'block'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse1" data = "{{item}}" /> </ block > </ view > </ block > <!--内联标签--> < view wx:else class = "{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse1" data = "{{item}}" /> </ block > </ view > </ block > <!--判断是否是文本节点--> < block wx:elif = "{{item.node == 'text'}}" > <!--如果是,直接进行--> < template is = "WxEmojiView" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse1" > <!--<template is="wxParse2" data="{{item}}" />--> <!--判断是否是标签节点--> < block wx:if = "{{item.node == 'element'}}" > < block wx:if = "{{item.tag == 'button'}}" > < button type = "default" size = "mini" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse2" data = "{{item}}" /> </ block > </ button > </ block > <!--li类型--> < block wx:elif = "{{item.tag == 'li'}}" > < view class = "{{item.classStr}} wxParse-li" style = "{{item.styleStr}}" > < view class = "{{item.classStr}} wxParse-li-inner" > < view class = "{{item.classStr}} wxParse-li-text" > < view class = "{{item.classStr}} wxParse-li-circle" ></ view > </ view > < view class = "{{item.classStr}} wxParse-li-text" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse2" data = "{{item}}" /> </ block > </ view > </ view > </ view > </ block > <!--video类型--> < block wx:elif = "{{item.tag == 'video'}}" > < template is = "wxParseVideo" data = "{{item}}" /> </ block > <!--img类型--> < block wx:elif = "{{item.tag == 'img'}}" > < template is = "wxParseImg" data = "{{item}}" /> </ block > <!--a类型--> < block wx:elif = "{{item.tag == 'a'}}" > < view bindtap = "wxParseTagATap" class = "wxParse-inline {{item.classStr}} wxParse-{{item.tag}}" data-src = "{{item.attr.href}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse2" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'br'}}" > < template is = "WxParseBr" ></ template > </ block > <!--其他块级标签--> < block wx:elif = "{{item.tagType == 'block'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse2" data = "{{item}}" /> </ block > </ view > </ block > <!--内联标签--> < view wx:else class = "{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse2" data = "{{item}}" /> </ block > </ view > </ block > <!--判断是否是文本节点--> < block wx:elif = "{{item.node == 'text'}}" > <!--如果是,直接进行--> < template is = "WxEmojiView" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse2" > <!--<template is="wxParse3" data="{{item}}" />--> <!--判断是否是标签节点--> < block wx:if = "{{item.node == 'element'}}" > < block wx:if = "{{item.tag == 'button'}}" > < button type = "default" size = "mini" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse3" data = "{{item}}" /> </ block > </ button > </ block > <!--li类型--> < block wx:elif = "{{item.tag == 'li'}}" > < view class = "{{item.classStr}} wxParse-li" style = "{{item.styleStr}}" > < view class = "{{item.classStr}} wxParse-li-inner" > < view class = "{{item.classStr}} wxParse-li-text" > < view class = "{{item.classStr}} wxParse-li-circle" ></ view > </ view > < view class = "{{item.classStr}} wxParse-li-text" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse3" data = "{{item}}" /> </ block > </ view > </ view > </ view > </ block > <!--video类型--> < block wx:elif = "{{item.tag == 'video'}}" > < template is = "wxParseVideo" data = "{{item}}" /> </ block > <!--img类型--> < block wx:elif = "{{item.tag == 'img'}}" > < template is = "wxParseImg" data = "{{item}}" /> </ block > <!--a类型--> < block wx:elif = "{{item.tag == 'a'}}" > < view bindtap = "wxParseTagATap" class = "wxParse-inline {{item.classStr}} wxParse-{{item.tag}}" data-src = "{{item.attr.href}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse3" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'br'}}" > < template is = "WxParseBr" ></ template > </ block > <!--其他块级标签--> < block wx:elif = "{{item.tagType == 'block'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse3" data = "{{item}}" /> </ block > </ view > </ block > <!--内联标签--> < view wx:else class = "{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse3" data = "{{item}}" /> </ block > </ view > </ block > <!--判断是否是文本节点--> < block wx:elif = "{{item.node == 'text'}}" > <!--如果是,直接进行--> < template is = "WxEmojiView" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse3" > <!--<template is="wxParse4" data="{{item}}" />--> <!--判断是否是标签节点--> < block wx:if = "{{item.node == 'element'}}" > < block wx:if = "{{item.tag == 'button'}}" > < button type = "default" size = "mini" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse4" data = "{{item}}" /> </ block > </ button > </ block > <!--li类型--> < block wx:elif = "{{item.tag == 'li'}}" > < view class = "{{item.classStr}} wxParse-li" style = "{{item.styleStr}}" > < view class = "{{item.classStr}} wxParse-li-inner" > < view class = "{{item.classStr}} wxParse-li-text" > < view class = "{{item.classStr}} wxParse-li-circle" ></ view > </ view > < view class = "{{item.classStr}} wxParse-li-text" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse4" data = "{{item}}" /> </ block > </ view > </ view > </ view > </ block > <!--video类型--> < block wx:elif = "{{item.tag == 'video'}}" > < template is = "wxParseVideo" data = "{{item}}" /> </ block > <!--img类型--> < block wx:elif = "{{item.tag == 'img'}}" > < template is = "wxParseImg" data = "{{item}}" /> </ block > <!--a类型--> < block wx:elif = "{{item.tag == 'a'}}" > < view bindtap = "wxParseTagATap" class = "wxParse-inline {{item.classStr}} wxParse-{{item.tag}}" data-src = "{{item.attr.href}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse4" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'br'}}" > < template is = "WxParseBr" ></ template > </ block > <!--其他块级标签--> < block wx:elif = "{{item.tagType == 'block'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse4" data = "{{item}}" /> </ block > </ view > </ block > <!--内联标签--> < view wx:else class = "{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse4" data = "{{item}}" /> </ block > </ view > </ block > <!--判断是否是文本节点--> < block wx:elif = "{{item.node == 'text'}}" > <!--如果是,直接进行--> < template is = "WxEmojiView" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse4" > <!--<template is="wxParse5" data="{{item}}" />--> <!--判断是否是标签节点--> < block wx:if = "{{item.node == 'element'}}" > < block wx:if = "{{item.tag == 'button'}}" > < button type = "default" size = "mini" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse5" data = "{{item}}" /> </ block > </ button > </ block > <!--li类型--> < block wx:elif = "{{item.tag == 'li'}}" > < view class = "{{item.classStr}} wxParse-li" style = "{{item.styleStr}}" > < view class = "{{item.classStr}} wxParse-li-inner" > < view class = "{{item.classStr}} wxParse-li-text" > < view class = "{{item.classStr}} wxParse-li-circle" ></ view > </ view > < view class = "{{item.classStr}} wxParse-li-text" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse5" data = "{{item}}" /> </ block > </ view > </ view > </ view > </ block > <!--video类型--> < block wx:elif = "{{item.tag == 'video'}}" > < template is = "wxParseVideo" data = "{{item}}" /> </ block > <!--img类型--> < block wx:elif = "{{item.tag == 'img'}}" > < template is = "wxParseImg" data = "{{item}}" /> </ block > <!--a类型--> < block wx:elif = "{{item.tag == 'a'}}" > < view bindtap = "wxParseTagATap" class = "wxParse-inline {{item.classStr}} wxParse-{{item.tag}}" data-src = "{{item.attr.href}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse5" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'br'}}" > < template is = "WxParseBr" ></ template > </ block > <!--其他块级标签--> < block wx:elif = "{{item.tagType == 'block'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse5" data = "{{item}}" /> </ block > </ view > </ block > <!--内联标签--> < view wx:else class = "{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse5" data = "{{item}}" /> </ block > </ view > </ block > <!--判断是否是文本节点--> < block wx:elif = "{{item.node == 'text'}}" > <!--如果是,直接进行--> < template is = "WxEmojiView" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse5" > <!--<template is="wxParse6" data="{{item}}" />--> <!--判断是否是标签节点--> < block wx:if = "{{item.node == 'element'}}" > < block wx:if = "{{item.tag == 'button'}}" > < button type = "default" size = "mini" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse6" data = "{{item}}" /> </ block > </ button > </ block > <!--li类型--> < block wx:elif = "{{item.tag == 'li'}}" > < view class = "{{item.classStr}} wxParse-li" style = "{{item.styleStr}}" > < view class = "{{item.classStr}} wxParse-li-inner" > < view class = "{{item.classStr}} wxParse-li-text" > < view class = "{{item.classStr}} wxParse-li-circle" ></ view > </ view > < view class = "{{item.classStr}} wxParse-li-text" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse6" data = "{{item}}" /> </ block > </ view > </ view > </ view > </ block > <!--video类型--> < block wx:elif = "{{item.tag == 'video'}}" > < template is = "wxParseVideo" data = "{{item}}" /> </ block > <!--img类型--> < block wx:elif = "{{item.tag == 'img'}}" > < template is = "wxParseImg" data = "{{item}}" /> </ block > <!--a类型--> < block wx:elif = "{{item.tag == 'a'}}" > < view bindtap = "wxParseTagATap" class = "wxParse-inline {{item.classStr}} wxParse-{{item.tag}}" data-src = "{{item.attr.href}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse6" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'br'}}" > < template is = "WxParseBr" ></ template > </ block > <!--其他块级标签--> < block wx:elif = "{{item.tagType == 'block'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse6" data = "{{item}}" /> </ block > </ view > </ block > <!--内联标签--> < view wx:else class = "{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse6" data = "{{item}}" /> </ block > </ view > </ block > <!--判断是否是文本节点--> < block wx:elif = "{{item.node == 'text'}}" > <!--如果是,直接进行--> < template is = "WxEmojiView" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse6" > <!--<template is="wxParse7" data="{{item}}" />--> <!--判断是否是标签节点--> < block wx:if = "{{item.node == 'element'}}" > < block wx:if = "{{item.tag == 'button'}}" > < button type = "default" size = "mini" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse7" data = "{{item}}" /> </ block > </ button > </ block > <!--li类型--> < block wx:elif = "{{item.tag == 'li'}}" > < view class = "{{item.classStr}} wxParse-li" style = "{{item.styleStr}}" > < view class = "{{item.classStr}} wxParse-li-inner" > < view class = "{{item.classStr}} wxParse-li-text" > < view class = "{{item.classStr}} wxParse-li-circle" ></ view > </ view > < view class = "{{item.classStr}} wxParse-li-text" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse7" data = "{{item}}" /> </ block > </ view > </ view > </ view > </ block > <!--video类型--> < block wx:elif = "{{item.tag == 'video'}}" > < template is = "wxParseVideo" data = "{{item}}" /> </ block > <!--img类型--> < block wx:elif = "{{item.tag == 'img'}}" > < template is = "wxParseImg" data = "{{item}}" /> </ block > <!--a类型--> < block wx:elif = "{{item.tag == 'a'}}" > < view bindtap = "wxParseTagATap" class = "wxParse-inline {{item.classStr}} wxParse-{{item.tag}}" data-src = "{{item.attr.href}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse7" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'br'}}" > < template is = "WxParseBr" ></ template > </ block > <!--其他块级标签--> < block wx:elif = "{{item.tagType == 'block'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse7" data = "{{item}}" /> </ block > </ view > </ block > <!--内联标签--> < view wx:else class = "{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse7" data = "{{item}}" /> </ block > </ view > </ block > <!--判断是否是文本节点--> < block wx:elif = "{{item.node == 'text'}}" > <!--如果是,直接进行--> < template is = "WxEmojiView" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse7" > <!--<template is="wxParse8" data="{{item}}" />--> <!--判断是否是标签节点--> < block wx:if = "{{item.node == 'element'}}" > < block wx:if = "{{item.tag == 'button'}}" > < button type = "default" size = "mini" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse8" data = "{{item}}" /> </ block > </ button > </ block > <!--li类型--> < block wx:elif = "{{item.tag == 'li'}}" > < view class = "{{item.classStr}} wxParse-li" style = "{{item.styleStr}}" > < view class = "{{item.classStr}} wxParse-li-inner" > < view class = "{{item.classStr}} wxParse-li-text" > < view class = "{{item.classStr}} wxParse-li-circle" ></ view > </ view > < view class = "{{item.classStr}} wxParse-li-text" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse8" data = "{{item}}" /> </ block > </ view > </ view > </ view > </ block > <!--video类型--> < block wx:elif = "{{item.tag == 'video'}}" > < template is = "wxParseVideo" data = "{{item}}" /> </ block > <!--img类型--> < block wx:elif = "{{item.tag == 'img'}}" > < template is = "wxParseImg" data = "{{item}}" /> </ block > <!--a类型--> < block wx:elif = "{{item.tag == 'a'}}" > < view bindtap = "wxParseTagATap" class = "wxParse-inline {{item.classStr}} wxParse-{{item.tag}}" data-src = "{{item.attr.href}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse8" data = "{{item}}" /> </ block > </ view > </ block > < block wx:elif = "{{item.tag == 'br'}}" > < template is = "WxParseBr" ></ template > </ block > <!--其他块级标签--> < block wx:elif = "{{item.tagType == 'block'}}" > < view class = "{{item.classStr}} wxParse-{{item.tag}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse8" data = "{{item}}" /> </ block > </ view > </ block > <!--内联标签--> < view wx:else class = "{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}" style = "{{item.styleStr}}" > < block wx:for = "{{item.nodes}}" wx:for-item = "item" wx:key = "" > < template is = "wxParse8" data = "{{item}}" /> </ block > </ view > </ block > <!--判断是否是文本节点--> < block wx:elif = "{{item.node == 'text'}}" > <!--如果是,直接进行--> < template is = "WxEmojiView" data = "{{item}}" /> </ block > </ template > <!--循环模版--> < template name = "wxParse8" > ... </ template > <!--循环模版--> < template name = "wxParse9" > ... </ template > <!--循环模版--> < template name = "wxParse10" > ... </ template > <!--循环模版--> < template name = "wxParse11" > ... </ template > |
所以是没人关注这个帖子吗?
没人注意就继续自顶
为了让官方人员看到,只能自己顶了
根源还在于不能动态创建元素,小程序的这个限制真的难受,完全静态化的东西,相当于一切页面都是提前设计好的,页面不存在动态的变更可能,但是程序所有页面都提前设计好,这怎么可能呢
确实,不能递归很麻烦。要手动写好多层
干吗一定要用这个组件呢,感觉不是非用不可啊
顶下,我现在是13个template,恶心,很难维护,一改就是得改13个
哈哈,是什么功能非要用那个wxparse组件
- - 富文本
我的项目上也遇到过这个问题,尝试过修改,但是依旧没有解决,这个template模板并不能递归调用。但是富文本,很多用户在使用的时候,会有大量的复制粘贴,层级上很有可能会超过12层。不晓得怎么搞了~
暂时是没办法解决。除非官方支持template模板递归调用
目前你只能继续再复制多几个template,template13, template14...
其实我觉得这个需求本身是一个非常有问题的“需求”,想当于类似热更新了。如果不是为了某些特殊理由,真的有必要这么做么。
这和热更新哪有类比性啊?明明是一个很正常的需求,类似于递归函数一样的需求
这和热更新完全没有类比性。这是一个实在需要的功能,类似于函数的递归调用,你能说函数递归调用没用吗
我着眼点是用来自网络的xml来更新界面,而不是一个本地化的数据来更新界面,意味着不但数据来自网络,显示内容的模式都不是事先定义好的。如果在iOS app的里面,这就妥妥的触犯了“通过网络更新程序在提交的时候不存在的功能”,当时热更新组件就是被这个理由名正言顺的被打压的。
界面本身由递归生成也不能算是一个很稳妥的方案,毕竟递归对内存不是一个很“安全”的方式。合理的情况应该是由递归算法产生数据,界面按生成的数据去显示,这个方案比较“安全”吧。
这里也不是用来自网络的xml来更新界面,其实就是写一段富文本描述字符串。。。
嗯,那就尴尬了。。。只能要求这个文本制作的时候不要超过嵌套层级限制了。。。。