当前位置:网站首页>话题功能实现
话题功能实现
2022-07-28 05:20:00 【可宇龙】
相信大家对话题都了解吧,比如微博的话题,点击话题展示该话题下的所有帖子


主要技术栈就是把话题替换为一个超链接标签可以点击
下面代码展示(python,tornado框架实现)>>>>>>>>
# 正则替换
async def change_object(self, sublist, stype):
re_dict = {
"talk": {"re": r'#([\w]+)#', "model": TalkModel, "column": "TalkModel.name.in_(items)",
"getkey": "myid.name", "replace": "#{}#", "a": "<a href='/talk?tid={}'>#{}#</a>"}
}
myre = re_dict[stype]["re"]
for x in sublist:
# 替换话题
items_dict = {}
items = re.findall(myre, x["content"])
ids = await self.database.execute(
re_dict[stype]["model"].select().where(eval(re_dict[stype]["column"])))
for myid in ids:
items_dict[eval(re_dict[stype]["getkey"])] = myid.id
for item in items:
try:
x["content"] = x["content"].replace(re_dict[stype]["replace"].format(item),
re_dict[stype]["a"].format(items_dict[item], item))
except Exception as e:
print(str(e))
pass
return sublist此代码,利用了封装的思路,将替换封装为一个方法,以便调用,减少代码的复用!
替换后前端将获取到的替换内容使用v-html转换为标签即可,通过点击超链接获取到指定标签id,来获取该标签下的所有内容!
类似这种需要替换的还有好多,@好友也是同样原理,感兴趣的小伙伴可以去试试
边栏推荐
猜你喜欢
随机推荐
结果填空 凑算式(DFS*C语言)
Annotation and grid addition of ArcGIS map making
Making E-R diagram based on XMIND
【uni-app】uni-app中scroll-into-view的使用
Online word cloud generation (taking WordArt as an example)
ArcGIS之Model Builder
Progressive enhancement and graceful degradation
DOM模型的相关概念和操作
书籍-乌合之众
DOM——事件
微信公众号-授权登录
JS中的!!
null和undefined的区别
(php毕业设计)基于php在线旅游网站管理系统获取
(php毕业设计)基于php水果销售商店管理系统获取
Review of metallurgical physical chemistry --- electrodeposition and reduction process of metals
Sankey diagram drawing based on highcharts platform
(php毕业设计)基于php学生作业提交管理系统获取
在线词云图生成(以WordArt为例)
uniapp uview组件库的使用方法(下载方式)



![[uni app] the use of scroll into view in uni app](/img/34/475ef70416df7a92a52ba0efbc8947.png)




