Advancing

Custom templates

qianmoQqianmoQ· 更新于 2026-09-15· 阅读 10 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

Custom templates

Override HTML templates, static files, and inject custom content

Overriding full HTML page templates such as `custom/templates/home.tmpl` is deprecated starting with 0.15.0. The web interface is being rebuilt as a React application that renders pages on the client, so it no longer reads page templates from the `templates/` directory and your copies of them will stop taking effect once the migration is complete.

Content injection through inject/head.tmpl and inject/footer.tmpl, static file overrides under custom/public/, and email template overrides all remain fully supported.

Gogs allows you to customize the appearance and behavior of your instance by overriding HTML templates, replacing static files, and injecting custom content. All customizations are placed under the custom/ directory and survive code updates.

警告

Be careful when overriding templates and static files, as changes to the upstream Gogs codebase may break your customizations in future releases. Keep track of what you have overridden.

Override HTML templates

You can replace any HTML template (including email templates) by placing a customized version under the custom/templates/ directory.

  1. 1
    Find the original template

    Locate the template file you want to customize in the templates/ directory of the Gogs source code. For example, to customize the home page, find templates/home.tmpl.

  2. 2
    Copy and edit

    Copy the content of the template file and save your edited version to the corresponding path under custom/templates/. For example:

    custom/templates/home.tmpl
  3. 3
    Restart Gogs

    Edits to custom HTML templates require restarting Gogs to take effect.

警告

Override for email templates is disabled when [server] LOAD_ASSETS_FROM_DISK = true is set in your configuration. If you are using this setting, email template overrides will not be applied.

Override static files

You can replace static files (CSS, JavaScript, images, etc.) by placing customized versions under the custom/public/ directory.

For example, to override the site favicon, place your version at:

custom/public/img/favicon.png
提示

Edits to custom static files do not require restarting Gogs. Changes take effect immediately.

Inject custom content

You can inject custom HTML into the head or footer of every page without touching the main repository source code. This is useful for adding analytics code, custom stylesheets, or other static resources.

This approach is recommended whenever possible because it has the minimum impact on templates and is less likely to break during upgrades.

The injection points are:

File Location Purpose
custom/templates/inject/head.tmpl Inside <head> Add stylesheets, meta tags, analytics scripts
custom/templates/inject/footer.tmpl Before </body> Add scripts, tracking code, custom footer content
警告

With the React web interface, injection files no longer evaluate Go template expressions such as {{.User}}. Their contents are inserted into every page verbatim. If your existing head.tmpl or footer.tmpl uses Go template expressions, replace them with plain HTML when migrating to the React web interface.

Example: custom CSS file

The following example shows how to include a custom CSS file in your Gogs instance:

  1. 1
    Create the CSS file

    Create a file named custom.css under the custom/public/css/ directory:

    custom/public/css/custom.css
  2. 2
    Add your CSS rules

    Write your CSS rules in the file. For example:

    /* custom/public/css/custom.css */
    .dashboard .news .news-item .header {
      color: #333;
    }
    
    footer {
      background-color: #f5f5f5;
    }
  3. 3
    Link the stylesheet

    Edit the file custom/templates/inject/head.tmpl and add a link to your CSS file:

    <link rel="stylesheet" href="/css/custom.css">
  4. 4
    Restart Gogs

    Restart Gogs to load the new head.tmpl injection template. A production server reads the injection templates once at startup, so later edits to head.tmpl or footer.tmpl require another restart. Edits to the custom CSS file it links do not require a restart.

评论

登录后参与评论

正在加载评论…