主题开发者快速参考指南

随着主题变得越来越强大,关于它们如何工作的需要记住的内容也越来越多。我们在 #howto / #themes 下有大量详细的文档,但如果您只是需要一些能提醒您记忆的东西,本指南可能会有所帮助。

一般资源

:scroll: 初学者指南
:scroll: 设计师指南
:scroll: 开发者指南
:paintbrush: 主题创建器
:desktop_computer: 主题 CLI
:notebook_with_decorative_cover: 主题目录
:jigsaw: 组件目录
:wrench: 主题修改器
:wrench: 可主题化的站点设置

文件/文件夹结构 阅读更多

about.json
settings.yml
common/, desktop/, mobile/
  {common|desktop|mobile}.scss
  head_tag.html
  header.html
  after_header.html
  body_tag.html
  footer.html
  embedded.scss (仅 common)
locales/
  en.yml
  ...
assets/
  (任意命名的文件,在 about.json 中引用)
stylesheets/
  (任意命名的文件,可以从其他文件以及 common/desktop/mobile.scss 中导入)
javascripts/
  (任意命名的文件。支持 .js .hbs 和 .raw.hbs)

about.json 结构信息, 可用元数据

{
  "name": "我的主题",
  "component": false,
  "license_url": null,
  "about_url": null,
  "authors": null,
  "theme_version": null,
  "minimum_discourse_version": null,
  "maximum_discourse_version": null,
  "assets": {
    "variable-name": "assets/my-asset.jpg"
  },
  "color_schemes": {
    "我的配色方案": {
      "primary": "222222"
    }
  }
}

SCSS

:link: 可用的 CSS 变量

Javascript 阅读更多

// {theme}/javascripts/api-initializers/init-theme.gjs
import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  // 您的代码
});

:link: JS 插件 API

:link: 多文件 Javascript

设置 阅读更多

settings.yml:

fruit:
  default: apples|oranges
  type: list
  description: # 旧方法。最好在语言文件(见下文)中定义这些
    en: English Description
    fr: Description Française

从 JavaScript 访问:

console.log(settings.fruit);

从 gjs 模板访问:

<template>{{settings.fruit}}</template>

从 scss 访问:

html {
  font-size: #{$global-font-size}px;
  background: $site-background;
}

可主题化的站点设置 阅读更多

about.json:

"theme_site_settings": {
  "enable_welcome_banner": false
}

从 JavaScript 访问:

@service siteSettings;

this.siteSettings.enable_welcome_banner;

从 gjs 模板访问:

<template>{{this.siteSettings.enable_welcome_banner}}</template>

翻译 阅读更多

locales/en.yml

en:
  my_translation_key: "I love themes"
  theme_metadata: # 这些用于管理面板。它们不会在您的 js/hbs 文件中提供
    description: 此主题可让您在 Discourse 上做惊人的事情
    settings:
      fruit: whitelisted_fruits 设置的描述

从 JavaScript 访问:

import { i18n } from "discourse-i18n";
i18n(themePrefix("my_translation_key"));

从 gjs 模板访问:

import { i18n } from "discourse-i18n";

<template>
  {{i18n (themePrefix "my_translation_key")}}
  <DButton @label={{theme-prefix "my_translation_key"}} />
</template>

本文档是版本控制的 - 在 github 上 建议更改。

40 个赞

This is super useful.

Could I also suggest that a useful reference to add is the list of available color transformations:

3 个赞

I must be missing something here, I’m trying to use I18n.t(themePrefix("my_translation_key")) but the Firefox console reports that themePrefix is undefined.
How can I invoke that function from an api function?

该链接似乎不再有效。

2 个赞

你说得对。现在应该是:

https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/plugin-api.js

2 个赞

感谢 @th21@Arkshine - 我已更新链接

2 个赞

插件 API 链接又变了:

https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/plugin-api.gjs

2 个赞

谢谢!已更新 :writing_hand:

3 个赞