Include assets (e.g. images, fonts) in themes and components

Themes and theme components allow you to handle uploaded assets such as images and fonts. You can control what assets your theme accepts using the site setting: theme authorized extensions

Including assets in themes and components

For remote themes

Remote themes include:

  • Themes and components installed from the Popular list of themes
  • Themes and components installed using the From a git repository method

To upload assets to a remote theme or theme component, see Create and share a font theme component for a detailed example.

Uploading assets to local themes and components

Local themes include:

  • The default Light and Dark themes that ship with every Discourse instance
  • Themes and components created using the + Create New installation method
  • Themes and components that were uploaded from your local computer using the From your device installation method.

To upload assets to a local theme or theme component, navigate to your theme or component and select + Add in the Uploads section:

In the Add Upload modal, choose a file and enter a SCSS variable name to use with with your CSS, javascript, and/or handlebars customizations

Once uploaded, the asset will pop up in the Uploads list:

:warning: Important Note: Do not add uploads to themes and components through the admin interface if the component was installed remotely from a git repository. Updates to the component will clear out any uploads that were not included in the git repository.

How to make use of the assets

SCSS

@font-face {
  font-family: Amazing;
  src: url($Comic-Sans) format("woff2");
}

body {
  font-family: Amazing;
  font-size: 15px;
}

.d-header {
  background-image: url($texture);
}

:informationsource: _When using ttf or otf fonts, be sure to use a format of opentype.

Javascript

// {theme}/javascripts/discourse/api-initializers/init-theme.gjs

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  console.log(settings.theme_uploads.balloons);
});

HTML

You can’t directly add a theme asset to vanilla HTML (like in the header or after header sections of the customize admin panel). You have to use a handlebars template or the plugin API (more about those in Developing Discourse Themes & Theme Components).

One workaround is to load your asset as a background image using SCSS (like the example above). So in the header section of your theme you might add:

<div class="my-custom-div">
  <a href="/"></a>
</div>

and then in your CSS:

.my-custom-div a {
  height: 50px;
  width: 100px;
  background-image: url($asset-name);
}

Accessing theme uploads as settings

You may also treat theme_uploads as settings in JavaScript (related commit).

This allows themes and components access to theme assets.

Inside theme js you can now get the URL for an asset with:

settings.theme_uploads.name

Additional Information


This document is version controlled - suggest changes on github.

30개의 좋아요

소셜 미디어 아이콘을 위해 5개의 SVG 파일이 포함된 assets 디렉터리를 생성했습니다. Create and Share a font theme component 문서에 따라 모든 파일을 about.json에 추가했습니다. CSS에서 body_tag.html을 통해 이 아이콘들을 사용하고 있습니다.

기대 결과: 아이콘이 로드되어야 합니다.

실제 결과: 아이콘이 표시되지 않습니다. 개발자 도구의 DevTools에서는 브라우저가 등록된 이미지로 인식하고 있는 것으로 보이지만, 실제 데이터는 없습니다.

무엇을 놓치고 있는 걸까요? 빌드 단계나 다른 작업이 필요한 건가요?

1개의 좋아요