您好,
我尝试在特定类别中添加免责声明。我的想法是有一个“横幅”,例如:
“这是用户的第一篇帖子 - 让我们欢迎新成员加入我们的社区!”
或者
“我们已经很久没见到用户了 - 上一篇帖子是一年前。”
在每篇帖子上方。
我尝试通过以下方式实现:
common/head_tag.html
<script type="text/discourse-plugin" version="0.8">
api.decorateWidget('post:after', (helper, args) => {
if (args.topic.category_id === 51) { // 我的类别
const $post = helper.find('.cooked');
const disclaimerText = api.decorateCooked(helper, args, settings.MY_DISCLAIMER);
$post.prepend(`<div class="disclaimer">${disclaimerText}</div>`);
}
});
</script>
为了完整起见,这里是
settings.yml
MY_DISCLAIMER:
type: string
default: 'This is a Category that comes with limited warranty and is to be used at your own risk.'
description: Disclaimer Text for Category
并且还有一个 common/common.scss 文件,其中包含 .disclaimer 类。
但是,当我激活它时,帖子本身不再显示,内容区域只是空白。
感谢任何帮助!
1 个赞
您好,欢迎回来!
如果看到空白,那是因为您遇到了错误。helper 中不存在 find。
通常可以在浏览器控制台 (F12) 中看到。
您可能想要类似这样的代码:
api.decorateWidget('post:before', (helper, args) => {
if (helper.widget.model.topic.category_id === 51) {
return helper.h("div.disclaimer", settings.MY_DISCLAIMER)
}
});
或者使用原始 HTML:
const RawHtml = require("discourse/widgets/raw-html").default;
api.decorateWidget('post:before', (helper, args) => {
if (helper.widget.model.topic.category_id === 51) {
return new RawHtml({html: `<div class="container">${settings.MY_DISCLAIMER}</div>`})
}
});
您可以阅读这篇帖子 ,了解这个 h 助手是如何工作的。
如果您想在已烹饪内容之前添加一些 HTML,可以这样做:
api.decorateWidget('post-contents:before', (helper, args) => {
const { topic } = helper.widget.findAncestorModel();
if (topic && topic.category_id === 51) {
return helper.h("div.disclaimer", settings.MY_DISCLAIMER);
}
});
3 个赞
感谢 @Arkshine ,这很有帮助!
我使用了以下代码,因为我想像其他提到的横幅一样将文本显示在所有内容之上。
api.decorateWidget('post:before', (helper, args) => {
if (helper.widget.model.topic.category_id === 51) {
return helper.h("div.disclaimer", settings.MY_DISCLAIMER)
}
});
致以问候!
1 个赞
system
(system)
关闭
2023 年6 月 1 日 08:49
5
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.