# 为标签添加一个 data-tag-group 属性

**URL:** <https://meta.discourse.org/t/add-a-data-tag-group-attribute-for-tags/234410>\
**Category:** Feature\
**Tags:** tag-groups\
**Created:** [2022年七月29日 14:58 UTC](https://meta.discourse.org/t/add-a-data-tag-group-attribute-for-tags/234410 "2022-07-29T14:58:25Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)\
**Post date:** [2025年十月20日 19:47 UTC](https://meta.discourse.org/t/add-a-data-tag-group-attribute-for-tags/234410/12 "2025-10-20T19:47:08Z")

</div>

只是想分享一下，这个功能已经有人着手开发了，但后来发现这是一个很大的改动，所以关闭了。这是一个很棒的想法，但不幸的是，团队近期不太可能采纳它。

> <https://github.com/discourse/discourse/pull/27949>
>
> \## Overview
> 
> This commit adds the \`data-tag-groups\` attribute to tags, making …it easier to use CSS selectors to batch select tags belonging to certain tag groups.
> 
> An example:
> 
> \`\`\`html
> \<a href="/tag/hidden" data-tag-name="hidden" data-tag-groups="Hidden Tag Group, non-hidden tags" class="discourse-tag box"\>hidden\</a\>
> \`\`\`
> 
> Related meta topic: https://meta.discourse.org/t/add-a-data-tag-group-attribute-for-tags/234410
> 
> \## Detailed changes
> 
> Add data-groups to these...
> \- \[\] Tags for topics
> - \[x\] The effect has been shown
> - \[\] Tests already written
> - \[\] Confirms that there are no performance issues
> \- \[\] TagChooser at the top of TopicList
> - \[x\] The effect has been shown
> - \[x\] Tests already written
> - \[\] Confirms that there are no performance issues
> \- \[\] Tags on the sidebar
> - \[x\] The effect has been shown
> - \[\] Tests already written
> - \[\] Confirms that there are no performance issues
> \- \[\] Tags on the \`/tags\` page
> - \[x\] The effect has been shown
> - \[\] Tests already written
> - \[\] Confirms that there are no performance issues
> \- \[\] Search bar tags
> - \[x\] The effect has been shown
> - \[\] Tests already written
> - \[\] Confirms that there are no performance issues
> \- \[\] Composer tags chooser
> - \[x\] The effect has been shown
> - \[\] Tests already written
> - \[\] Confirms that there are no performance issues
> \- \[\] ~~Tags in the post body~~ Maybe next PR
> 
> 
> \## DEV Improvements
> 
> Partially rewrote \`defaultRenderTag\` for better scalability and clarity:
> 
> The new \`defaultRenderTag\` reserves an interface for theme components to avoid the need for components to completely rewrite \`defaultRenderTag\` to provide custom tag renderer.
> 
> For example, you can now replace the tagRenderer like this:
> 
> \`\`\`javascript
> api.replaceTagRenderer((tag, params) =\> {
> return defaultRenderTag(tag, params, {
> extraClass: "custom-tag-class",
> contentFn(old) {
> return \`${iconHTML("tag")}${old}\`;
> },
> });
> });
> \`\`\`
> 
> This has a significant benefit for discourse tag icons. Since the \[old way of code\](https://github.com/discourse/discourse-tag-icons/blob/main/javascripts/discourse/initializers/tag-icons.js#L9-L84) the changes made by this PR will be completely overwritten by discourse tag icons, and the theme component must be updated accordingly.
> After applying the new interface, it can automatically synchronize changes with the \`defaultRendererTag\` in core.

---

_[View the full topic](https://meta.discourse.org/t/add-a-data-tag-group-attribute-for-tags/234410)._
