跳转到主要内容

快速参考

随着主题变得越来越强大,需要记住的工作方式也越来越多。我们在 #theme-guides 下提供了大量详细文档,但如果您只需要一些提示来唤起记忆,这份指南或许会有所帮助。

常规资源

: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 only)
locales/
  en.yml
  ...
assets/
  (arbitrarily named files, referenced in about.json)
stylesheets/
  (arbitrarily named files, can be imported from each other, and common/desktop/mobile.scss)
javascripts/
  (arbitrarily named files. Supports .js, .gjs and .hbs)

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

{
  "name": "My Theme",
  "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": {
    "My Color Scheme": {
      "primary": "222222"
    }
  }
}

SCSS

:link: 可用的 CSS 变量

Javascript 阅读更多

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

export default apiInitializer((api) => {
  // Your code here
});

:link: JS 插件 API

:link: 多文件 Javascript

设置 阅读更多

settings.yml:

fruit:
  default: apples|oranges
  type: list
  description: # Old method. It's better to define these in the locale files (see below)
    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: # These are used in the admin panel. They are not made available to your js/hbs files
    description: This theme lets you do amazing things on your Discourse
    settings:
      fruit: A description of the whitelisted_fruits setting

从 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>