开发话语主题及其组成部分

Discourse Themes and Theme Components can be used to customize the look, feel and functionality of Discourse’s frontend. This section of the developer guides aims to provide all the reference materials you need to develop simple themes for a single site, right up to complex open-source theme components.

This introduction aims to provide a map of all the tools and APIs for theme development. If you prefer a step-by-step tutorial for theme development, jump straight to:

Themes vs. Theme Components

Discourse Themes can be used to customize the frontend user experience using CSS and JavaScript. Each theme has its own git repository, and community admins can generally install and manage them via the Discourse admin panel, even on shared hosting platforms.

Theme Components are themes which are intended for use alongside other Theme Components, as part of an overall Theme. From a development point of view, Theme Components and Themes are almost identical. In these guides, the phrase “Theme” and “Theme Component” are used interchangeably.

Prerequisites

Firstly, make sure you understand how to use existing themes and theme components in Discourse. Using ready-made themes is the quickest and safest way to customize your community. If you need more, then it’s time to consider writing your own theme.

As part of Discourse’s overall architecture, Discourse Themes are built using standard HTML, CSS, JavaScript technologies, and make use of Ember concepts for more advanced UIs. These reference guides assume a base-level understanding of these technologies, and link out to external references where possible.

Discourse is a fast-moving project, and as such any custom theme will require maintenance over time. Make sure you consider this as part of your planning & development processes.

Getting Started

Frontend Customization

More!

Check out the rest of the Developer Guides !


This document is version controlled - suggest changes on github.

142 个赞
Get started with Theme Creator and the Theme CLI
Structure of themes and theme components
Theme Developer Quick Reference Guide
Executing js after DOM loaded
Request: House Ads Plugin
What do you use to make animated/gif instructions?
Is there any way to hyperlink to the middle of a post?
Getting Topic Information
Add long site description to about page
Add JS code to post?
Any options for over-riding the username restrictions?
My script is not working
Add CurrentThemeId into Body Css Classes
What's the fastest and easiest way to add a persistent javascript file or snippet
How to remove the number of months in the category section
How to inject HTML code in the head only on home and post pages
What do I need before signing up?
How to add JS in After Header?
Include assets (e.g. images, fonts) in themes and components
Versatile Banner
ItemList Schema Structured Data Error
Landing page customization
Where to place ad script?
Unable to use <script> with plugin outlets
Custom banner for discourse channel
Difficulties in correctly adding external JavaScript
Redirect Page for External URLs
How to have a Tag Column?
How do you add custom JS to a theme?
How can i add image in login and register box
Topic List Previews (legacy)
Adding Custom Background CSS
Changing layout with themes
Image not filling header container
Profile Image Looks Blurry After Resizing
Render modal from theme client-side plugin API
Is there an event that fires when topic-lists are reloaded?
Floating "New topic" button
Introducing Font Awesome 5 and SVG icons
Working on first plugin; does not work. Using the pirate-speak plugin as a base
Disable right click on images
Theme Creator Web Dev Help
Customize template used by post widget
Request modification homepage
How would updating effect custom overrides?
How to simplify DM's to individuals
Using CSS to suppress vote options from view results with zero votes
Click counter missing for some internal links
How to create custom theme component?
Can a Discourse theme/template be customised to have this format?
I would like to change the template completely
When you add an additional nesting level, all categories, including child categories, are displayed on the categories page
Make Tag Dropdown Retain Focus After Tag Picked; Client-Side API Questions
Add custom anonymous mode button
Introducing Font Awesome 5 and SVG icons
Can't add new class to topic page elements using jQuery?
Adding a second tag description
Change Default Avatar in home page (for original author avatar)
A native sidebar system
Topic list like on wyzecam
Advice for starting with Discourse development
How to remove the "topics _per week_" count?
Google Material icons
Modifying head_tag.html template
Integrate adbutler / Java script with the adplugin
Put user custom field on the user account page
Can you make Discourse look like medium.com?
Como faço para entender esse fórum? (How do I understand this forum?)
How to create a theme (if I am a pure end user)
How to create a theme (if I am a pure end user)
Override values for auto-generated color variables
Editing The 'Accept Invitation' Page Template
What HTML can you customize?
Wiki Threads - Linking Within A Single Post
reopenWidget throws error
How to add the two floating sidebar banners?
Add a "curated personal learning" page for users
Change "+ New Topic" button to match styling of "Reply" button
Question about modifying a theme we're using
Place for total coding beginners to learn how to customise?
Custom navigation way Classified fillter
Newbie help accessing code
Per-category Terms of Service
New lines in the composer's reply placeholder?
Fetch third party data for topic list
Discourse Docs
Discourse Category Headers theme component
Is there a theming 'map'? (An easy way to find plugin-outlets etc)
Call function on page change
Making custom changes to a raw template
Timeline overlapping "Suggested Topics"
Is it possible to modify a function in a helper via the plugin or theme component system?
Different category and sub-category order behaviour
Help me to append html code on Hamburger Section
Is there a way to overwrites all colors?
Customise Sign-up form design
Possible to share a post by save a picture with a QR code?
Dumb question: do I want to make a theme, plugin or component?
What are the different ways to customize content inside a post (custom attributes and such)
I’d like to start developing for Discourse, where should I begin?
Insert text or images anywhere on your site using CSS pseudo-elements
How to access the uncooked content from a theme component?
reopenWidget throws error
How to make a website with Discourse
How might we better structure #howto?
Best strategy for finding the right template to edit?
Is there a recommended way to get the current username from a post?
Theme-Component v Plugin: What's the difference
Theme-Component v Plugin: What's the difference
Theme-Component v Plugin: What's the difference
Minima Theme
How to organize code entered from the head tag?
Creating Parsing Extensions
Can't add new class to topic page elements using jQuery?
Can parameters be injected from the template to a widget action?
How to override a template.hbs from a plugin
A11y for Categories
Navigating to local URL fragment doesn't modify browser history
Wide image extending until search icon?
Customize "Create a New Topic " and "Create Topic" Button
How to add Google Custom Search Engine to Discourse
How to add Google Custom Search Engine to Discourse
Add Login / Signup Buttons to custom HTML
What's the fastest and easiest way to add a persistent javascript file or snippet
Sticky user card contents theme
Wordpress in header, Discourse in footer ? Both sticky
Ability to place custom fields in post header?
Topic List Previews (legacy)
How to put button in the corner of the screen?
Welcome page image
Custom header colour and category icons and login button
Theme Creator Broken Again?
Disabling full name changes
Can I change the URL of the main logo?
How to register a helper in Discourse?
Home page customization: latest + featured
Template overrides no longer working
Styling front page category title & nav container background
Any way to remove columns in the users page?
Category descriptions not showing up on mobile
Spawn navigation on topic pages
Selectively hiding numbers
Allow users to hide like counts
0 likes in latest section
0 likes in latest section
(old) method for adding languages to highlight.js on Discourse
Can I override app/views/layouts/application.html.erb from a plugin?
About the Theme category
About the Theme component category
How customize the appearance of sections like Amazon?
How to show HTML view and how to update data in HTML view
Confused about remotely loaded javascript content
api.decorateWidget - how can I find the template's names?
api.decorateWidget - how can I find the template's names?
Autocompletion for a user field at signup
Is there a way to add a website navigation menu above Discourse header?
How can I add a menu item
Enhanced category-box display component
Interactive SVG using <object>?
Discourse for self hosting
FKB Pro - Social theme
Javascripts targeting the footer doesn't work after page transitions
How can I change the HTML template of advanced search?
JS widget/code (first post)
Next js application as custom front end replacement
Help needed with theming and logo
Is it possible to replace the h3 tag with another tag in category name titles?
Add project description to login page
Custom text at top of search results page
Landing page customization
How to add a custom url text link on the login page
Add settings to your Discourse theme
Custom discourse with limited features
[PAID] Onedrive file picker theme component
How can I call third party apis in discourse
How to correctly listen on post-stream update
Modifying DirectoryTables
How to add Javascript to Footer Of Posts?
Website Integration
Is there a comprehensive list of customizable components?
Need help to emulate old forum
How to show Full Name in Embeds
How to display full name in comments embeds
How can I change the background of a certain topic?
Disable media download in onebox
How to bring in Latest topics when using category boxes
Banner text customization for Subscriptions plugin
How do I create a new Sign Up / Login Button?
Login modal misaligned due to ratings plugin
How do I get started with Discourse development as a regular end user with no programming experience?
Lag in selecting text
Big vBulletin forum migration project
Air Theme
What is the name of this theme?
How to customize discourse composer editor
User profile page / CSS class for groups / how to add it to page?
Insert the hbs template into the plugin outlet on the page -> user-preferences-interface to user-preferences-account
How to reopen header-contents widget?
Add localizable strings to themes and theme components
Edit Code
Add custom content on anywhere on discourse
Fakebook Theme
Add a custom header with a dropdown menu
Add settings to your Discourse theme
Contributing to Discourse
Ad Banner / HTML code in the right gutter
Changing the login Page for Custom Themes
How to get DOM elements and change onClick functionality
Designer's Guide to getting started with themes in Discourse
Making a search icon with a theme
Is there a way to change nav toggle menu on mobile to normal text like on desktop?
How to edit topics/show.html.erb
Links into the header block
Customize the header with links, icons, or menus
Import Sql file but can't see Custom theme folder in local development
Locations Plugin :earth_africa:
Author name plus avatar
Sticky footer to the bottom, pure CSS solution
The best way to make small ui modifications
Customize topic list entries (sorting, grouping, filtering) for better organization
Hamburger-categories widget does not mount?
Hamburger-categories widget does not mount?
Can I make a custom button with CSS and HTML?
Is it possible to change vis CSS the Category Moderator shield?
How to use themes?
How to put javascript in Discourse theme?
Implementing a countdown bbcode
Customize Your Site Branding
How can I pull the currently open topic's list of tags?
How can I pull the currently open topic's list of tags?
Any existing theme with categories on top as cards?
Deleting permissions | Create new Topic
Category page layout in 2 columns
Mobile View Loading different HTML as compared to Desktop
How can I have a gif be the embed thumbnail on a topic/post
Disable topic buttons converting to select on mobile
Is there a way to create a footer on the home with icons? (Only for mobile users, and overlapping the topics)
Can anyone help me with this home modification code?
How to make changes on backend side of discourse application using rails
Introduction to Discourse Development
How to add btn before "sign in"
Discourse blog (article and comments)
Custom tags?
Chat look customization
Minimizing Maintenance on Theme Customizations
Sidebar link / top button to PM admin
Can we delete all 'Automatically Bumped' msgs in one go?
Why no view count on mobile
Adding custom html under header (d-header-wrap) instead of main-outlet
How to pass a component setting as a value to an attribute?
Show login first on invitation form
I want to learn how to program for Discourse
Cannot mount the "post-avatar" on the "topic-above-post-stream" plugin outlet
I need help to edit the sidebar
Help with Blocked words and custom button links
What Themes are being used for these Discourse forums?
How can one add a toggle button to the post menu and implement an action for that toggle button in a Discourse plugin?
How to copy the files/data/posts from production to Staging instance
Silent link seems can't handle #anchor links
Right Sidebar Blocks
New th in topic table
How to change category names via theme-component
How to develop new Theme
When to use a plugin vs theme component?
Silent link seems can't handle #anchor links
Is there a way to remove the pop-ups when making a new topic?
Looking for the perfect theme
How do I go about making a very customized theme?
Need help integrating code wrote on Edittext to the Discourse
Need help integrating code wrote on Edittext to the Discourse
I want to add adsense code in <head> for activet adsence account
How to change new topic's layout on mobile side?
JavaScript Execution Time
How do the `@import`'s work with SCSS?
Search section like on Reddit?
Air Theme
Gallery of discourse customization
Policy notice: no basic HTML / CSS questions please
How to resize 'Modern Category + Group Boxes' thumbnail via CSS
How can I change the main CSS codes?
How can I create a Discourse that's just like Stemaway.com?
Using api from custom component
Adding new columns to user lists in groups
Need help to **getSelected** and **replace** from Toolbarevent
How can I edit My Login Page
Embedding a whole Discourse forum on another site in an <iframe>
Error when clicking on the homepage logo
Developing Discourse Plugins - Part 1 - Create a basic plugin
How do I add category banners?
List of components or PluginOutlets with explanatory documentation
Develop a theme
Is it possible to change the icon for staff notices?
Customizing Sign-up and Log in
Handlebars template override all in the same html file?
Show sidebar to anonymous user + Welcome area in central theme
I'm trying to run discourse app locally in production env without docker
Challenges creating a custom color palette
Adding preview screenshots to Discourse themes
Theme Component for text over pics?
Webinar: Love Your Community
Sign Up button doesn't show up anymore with decorateWidget
Controlling site settings with themes
Customize user card
Why am I finding Discourse so difficult to customize?
Air Theme
Air Theme
Can you change the color of customizable text?
Add category moderators with the API?
How to create a Handlebar helper function
Need help setting up category list images
Self Service Badges?
Why are posts withdrawn instead of being deleted immediately?
How can I apply new changes when edit JS file in production?
Avatars by posts in Category with Featured Topics
Feature idea: Parameterized macros (or, "smart replacements")
Replacing Details Toolbar Button?
Feedback on my forum
Topic List Previews (legacy)
Customisability in discourse?
Applying a custom header to Discourse site
Add localizable strings to themes and theme components
How to add customer HTML after "post_1"
Add text to top navbar to the left of login/signup buttons
Add text to top navbar to the left of login/signup buttons
Extending the About page
Description of the category is not displayed at mobile version
Custom structure and more questions
Widget's user awareness
CSS selector to separate posts by current user
Which is Better? Discourse or Flarum?
Javascript assets in a theme component
Can I update the user field automatically via API or otherwise?
Help changing color of buttons
Using Discourse variables to personalize embedded Typeform
How to get specific category banners with subcategories listed below
Category name after category image
Have users complete form when creating a topic?
How difficult would it be to make the Discourse UI more like Flarum?
How do we add buttons next to the "New Topic" button?
Can I use a theme I created without allowing it to be used by others?
Discourse-webpack: A boilerplate for developing JS-heavy Discourse components
Need plugin code for customised dicourse header
How to make a newline in category description
Home page category lumping
How to make your side have a side-bar and external links

The PluginApi - modifyClass() section 部分有很多旧的或已失效的链接和引用……

这个文件已经改变了很多,checkReplyLength 甚至已经不存在了……

在同一部分的后面:

这个链接只会导致“404 - 页面未找到”错误……

之后我就放弃使用这些文档了……

4 个赞

帖子已拆分为新主题:用户卡片模板或小部件?

这部分的文档似乎有点过时了……(而且 api.modifyClass 抱怨缺少 pluginId)。我该如何在 about-page 加载时运行脚本?这个似乎不起作用(但也没有错误)。我在其他一些组件上尝试过,结果好坏参半。谢谢。

<script type="text/discourse-plugin" version="0.11.1">

api.modifyClass("component:about-page", {
  pluginId: 'discourse-about-theme',

  didInsertElement() {
    this._super(...arguments);
    console.log("Welcome to the about page!");
  }

});

</script>
1 个赞

您好 :wave: about-page 现在是 .gjs 格式,这也意味着它无法再以这种方式修改。如果您想进行更改,必须使用该页面上提供的连接器之一。

您可以使用 Plugin outlet locations theme component 轻松检查这些,或者在 \u003cPluginOutlet\u003e 文件中进行检查。

4 个赞

非常感谢。有道理。我改用了连接器,现在工作正常。

2 个赞

这份指南还准确吗?我通读了整份指南来尝试创建一个主题组件,然后查看了示例主题组件,发现它们都没有使用指南中的说明。例如,在查看 discourse-brand-header 主题组件时,它似乎已经从在 header.html 中使用 api.createwidget 重构为使用完全不同的代码,其中包含 initializers、Components 等。代码极其复杂。指南中提到了使用带有 widget 的 API 方法,但任何组件都根本不使用这些。是否有更新的指南或可以使用 Components 的东西?

编辑:据称 widget API 正在消失,但是否有关于改用什么的指南?这里的整个指南似乎都基于已弃用的代码?

3 个赞

确实如此。指南应该更新了。不过,有些部分仍然非常相关,比如文件的结构等等。

2 个赞

是的,SCSS 似乎是相关的,但除此之外,似乎所有示例主题组件都使用组件,并且已经重构为与此处教授的完全不同的代码库。我浏览了他们在插件 API 部分链接的所有组件,每一个都以一种与此处指南中所教的完全不同的方式进行了重构。小部件已不再使用。全部都是组件。是否有关于如何正确使用新的组件(因为小部件正在消失)的指南?

2 个赞

我刚刚看到了这个帖子,例如:Upcoming Header Changes - Preparing Themes and Plugins - 所以他们将所有内容从旧版小部件重构为 Glimmer 组件了吗?但是,本教程全是旧版小部件。Glimmer 组件到底是什么?有相关指南吗?在开始使用旧版小部件进行开发之前,我很高兴能及早发现这一点。也许在此指南中添加一个注释,说明小部件是旧版的,不应再使用?

2 个赞

好的,我看到 Glimmer 是新的 Ember 组件。但是,我看了一下 Discourse TOC 的代码,我的天……以前只需要大约 10 行代码的地方,现在却分散在许多文件中,有数百行代码,包含复杂的类、装饰器等。我猜 Ember 经历了一次 React 时刻,决定再次让事情变得非常复杂。看来主题组件不再像以前那样简单,拥有简单的 widget API,而是需要大量的编码……

2 个赞

@ddsgad 是的,你说得对,本指南大部分内容已严重过时。我最近一直在进行通用文档改进,希望在未来几周内更新/替换此指南。

正如你所提到的,像 DiscoTOC 这样的真实示例可以作为很好的参考。另外,这里有一些更新的特定指南:

关于 Ember 及其组件的通用信息,我建议查看 Ember 指南

6 个赞

感谢您提供的链接。非常有帮助。目前,我们基本上只在 discourse 主题(第一篇帖子)中自定义内容以实现更好的设计,因此我们基本上能够使用纯粹的 JavaScript(querySelectorAll 等)和 SCSS 来完成所有工作,所以我想我们目前将继续这样做。

3 个赞

OP 中冗长且过时的教程现已替换为更通用的介绍/指引信息,包括一个指向全新7步教程的链接,该教程遵循更现代的主题开发模式。

5 个赞

我已经全部读完了,我很喜欢!解释得很清楚,是非常好的介绍!

3 个赞

我喜欢这些指南!终于弄懂了 api.decorateCookedElement,再次感谢这些有用的文档!

4 个赞