如何在discourse论坛中特定主题的主题正文下添加唯一的HTML内容

我正在使用 discourse 论坛,需要在特定帖子的正文下方添加唯一的 HTML 内容。该 HTML 内容应仅在某些帖子中显示,而不是全部显示。

目前,我已使用以下代码添加了 HTML 内容:

<script type="text/x-handlebars" data-template-name="connectors/topic-area-bottom/custom-html">
  <div class="banner-control">
    <!-- Unique HTML content goes here -->
  </div>
</script>

此代码已添加到 connectors/topic-area-bottom outlet,但它在所有帖子中都显示了内容。我的目标是让此内容仅出现在特定帖子中,而不是每个帖子页面。

详细信息:
帖子页面没有像分类页面那样的唯一正文类。我需要一种方法来根据正在查看的帖子有条件地渲染此 HTML 内容。问题:如何在 Ember.js 中修改我的代码或设置,以确保仅在特定帖子中显示 topic-area-bottom outlet 下方的自定义 HTML 内容?是否有办法添加一个条件来检查特定的帖子 ID 或其他标识符?

任何指导或示例都将不胜感激!

this.model 包含主题数据,例如 idtitle 等。你可以使用它。

例如,要允许主题 ID 90 或 38,你可以这样写:

<script type="text/x-handlebars" data-template-name="connectors/topic-area-bottom/custom-html">
  {{#if (or (eq this.model.id 90) (eq this.model.id 38))}}
    <div class="banner-control">
      <!-- Unique HTML content goes here -->
    </div>
  {{/if}}
</script>

不过,我建议你使用 Theme CLI 并使用 split files

在那里添加更多条件或进行一些逻辑处理会很困难。可以使用 registerConnectorClass,但它已弃用。

现代方法是使用 renderInOutlet 和一个 glimmer 组件——例如,https://github.com/discourse/discourse-custom-header-links。
你说你想在特定的 outlet 中渲染:

你在这里定义了模板。请注意 this.shouldShow
https://github.com/discourse/discourse-custom-header-links/blob/main/javascripts/discourse/components/custom-header-links.hbs#L1

然后你可以在这里添加你的逻辑,来告诉组件是否显示:
https://github.com/discourse/discourse-custom-header-links/blob/main/javascripts/discourse/components/custom-header-links.js#L5-L7

希望这有帮助。如果你需要更多帮助,请告诉我。 :+1:

5 个赞

感谢您提供详细的解决方案,非常感谢。

2 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.