๐Ÿ“ฆ Template Packaging (Self-contained Packages)

Last updated๏ผš2026-11-25

๐Ÿ“ฆ Template Packaging Specification (Self-contained Template Package)

This chapter is for template developers: how to package a website template (HTML + images + CSS + JS) into a self-contained folder. Once placed into templates/, the customer can select it directly in the admin “Site Info โ†’ Template Path” dropdown; saving takes effect immediately, with no restart and no code changes.

๐ŸŽฏ Delivery Model

Role What to Do
Template developer (you) Build a complete template with all files in one folder and deliver that folder
Deployment / ops Place the entire folder under templates/ in the program directory
Customer Select it in the admin “Site Info โ†’ Template Path” dropdown โ†’ Save. Takes effect immediately

Multilingual sites may each select a different template: every site (including subsites) sets its own template path independently in “Site Management”. Language sites created via “One-click Enable Language Sites” come with a copy of the Chinese site’s template; modifying it affects only that language.

Using the template name mytheme as an example. The folder name is the template name โ€” also the name displayed in the admin dropdown, and the path segment of asset URLs.

templates/
โ””โ”€โ”€ mytheme/                  โ† Folder name = template name = tpl_path
    โ”œโ”€โ”€ index.html            โ† Home page (.html files must be at the root level!)
    โ”œโ”€โ”€ header.html           โ† Shared header
    โ”œโ”€โ”€ footer.html           โ† Shared footer
    โ”œโ”€โ”€ news.html             โ† Article list
    โ”œโ”€โ”€ news_detail.html      โ† Article detail
    โ”œโ”€โ”€ products.html         โ† Product list
    โ”œโ”€โ”€ product_detail.html   โ† Product detail
    โ”œโ”€โ”€ cases.html            โ† Case list
    โ”œโ”€โ”€ case_detail.html      โ† Case detail
    โ”œโ”€โ”€ category.html         โ† Category page
    โ”œโ”€โ”€ page_detail.html      โ† Single page
    โ”œโ”€โ”€ about.html
    โ”œโ”€โ”€ contact.html
    โ”œโ”€โ”€ error.html
    โ”œโ”€โ”€ icons.html
    โ”œโ”€โ”€ css/                  โ† Subdirectories may only hold assets, not .html
    โ”‚   โ””โ”€โ”€ style.css
    โ”œโ”€โ”€ js/
    โ”‚   โ””โ”€โ”€ main.js
    โ””โ”€โ”€ images/
        โ”œโ”€โ”€ logo.png
        โ””โ”€โ”€ banner.jpg

โš ๏ธ Four Hard Constraints (Must Follow)

1. All .html files must be at the root level of the template folder

The system loads templates with ParseGlob(templates/<template-name>/*.html), which does not recurse into subdirectories. Files placed in mytheme/pages/about.html will not be loaded. Subdirectories are only for assets such as css/js/images.

2. The folder must contain at least one .html, or it won’t appear in the dropdown

The admin dropdown lists only “real template packages”. A folder that only contains subdirectories with no .html at root level is skipped automatically (otherwise selecting it would silently fall back to the default template, appearing as “selected but nothing happened”).

3. Reserved names are unavailable: index and admin

templates/index/ is the base template (fallback layer), and templates/admin/ is the admin UI template. These two names are occupied by the system, won’t appear in the customer’s selection list, and should not be used as your template name.

4. Assets must be referenced with the absolute path /templates/<template-name>/โ€ฆ

See the next section. Relative paths (e.g. css/style.css) resolve incorrectly on multi-segment URLs such as detail pages.

๐Ÿ”— Asset Reference Conventions

css/js/images inside the template folder are served via a dedicated static route with the URL rule /templates/<template-name>/<relative-path>, mapping one-to-one to the disk structure.

<!-- Styles -->
<link rel="stylesheet" href="/templates/mytheme/css/style.css">

<!-- Scripts -->
<script src="/templates/mytheme/js/main.js"></script>

<!-- Images -->
<img src="/templates/mytheme/images/logo.png" alt="logo">

<!-- Relative paths are allowed inside CSS (relative to the css file's directory) -->
<!-- background: url(../images/banner.jpg); -->

The site-wide shared /static/โ€ฆ directory can still be used (the two coexist). Self-contained templates are recommended to use /templates/<template-name>/ exclusively, so the whole package can be copied and reused.

Allowed Asset Extensions (Whitelist)
Category Extensions
Styles / scripts .css .js .mjs
Images .png .jpg .jpeg .gif .svg .webp .bmp .ico .avif
Fonts .woff .woff2 .ttf .eot .otf
Audio / video .mp4 .webm .ogg .mp3 .wav
Data / documents .json .xml .txt .csv .pdf
โš ๏ธ ๆณจๆ„

Uploading and referencing are two different things: the table above lists extensions of static assets referencable from the front end. The admin “Upload Image” is more restricted, allowing only .jpg .jpeg .png .gif .webp .avif; .svg (XSS risk) and .bmp have been removed from the upload whitelist and cannot be uploaded via the admin (but can still be placed manually under /static/ as static assets and referenced). Uploaded images are automatically compressed and converted to AVIF in the browser (WebP if AVIF is unsupported) before submission, and the server performs real type validation as a fallback โ€” for the full mechanism, see the chapter Image Upload Security and AVIF Conversion.

Security note: .html and any non-whitelisted extension return 403 when accessed by URL, so template source cannot be downloaded; ../ path traversal is blocked as well. Therefore it is safe to place .html files and assets in the same folder.

๐Ÿงฑ Overlay Mechanism: Only Override the Files You Want to Change

At render time the system first loads the base template templates/index/, then overrides files with the same name using your template folder. So a template package does not need to include every page โ€” pages you don’t provide automatically fall back to the base template.

What Your Package Contains Actual Rendering Result
index.html + header.html + footer.html Home page, header, and footer use yours; other pages use the base template (but with your header/footer)
All 21 page files Your template takes over completely
Only index.html Only the home page has your styling; other pages keep the base template’s appearance

Recommendation: for a full delivery, override at least index.html, header.html, footer.html, and every list/detail page to avoid inconsistent styling.

The 21 Page Files Included in the Base Template (Overridable List)
index.html          header.html         footer.html         icons.html
news.html           news_detail.html    products.html       product_detail.html
cases.html          case_detail.html    category.html       page_detail.html
about.html          contact.html        error.html          tag.html
jobs.html           job_detail.html     search.html         languages.html
media_gallery.html

๐Ÿš€ Minimal Working Template Package Example

A minimal package that only changes the home page styling โ€” three files are enough to run.

<!-- templates/mytheme/index.html -->
<!DOCTYPE html>
<html lang="{{.SiteLanguage}}">
<head>
  <meta charset="UTF-8">
  <title>{{.PageTitle}}</title>
  <meta name="keywords" content="{{.PageKeywords}}">
  <meta name="description" content="{{.PageDescription}}">
  <link rel="stylesheet" href="/templates/mytheme/css/style.css">
</head>
<body>
  {{template "header.html" .}}

  <img src="/templates/mytheme/images/logo.png" alt="{{.SiteTitle}}">

  <ul class="news-list">
  {{range list "type=news&num=8&order=date"}}
    <li>
      <a href="{{.URL}}">{{.Title}}</a>
      <time>{{.CreatedAt | formatDate "Y-m-d"}}</time>
    </li>
  {{end}}
  </ul>

  {{template "footer.html" .}}
  <script src="/templates/mytheme/js/main.js"></script>
</body>
</html>

Two key points in the example: detail links must use {{.URL}} (it carries the site prefix, required for multilingual subsites), and assets use absolute paths /templates/mytheme/โ€ฆ.

๐Ÿ“ฅ Installation and Switching Steps

Step Action
1 Copy the entire template folder (e.g. mytheme/) into templates/ in the program directory
2 Admin โ†’ “Site Info” โ†’ “Template Path” dropdown, select mytheme
3 Click Save. The system automatically reloads templates, no process restart needed
4 Refresh the front end to see the result (if page caching is enabled, wait about 1 minute or clear the cache in the admin)

Multi-site scenario: select the template path separately for each site in “Site Management”; language sites may use different templates. Language sites created via one-click enable already have an independent template directory โ€” no manual copying needed.

๐Ÿ” Troubleshooting

Symptom Cause and Solution
My template doesn’t appear in the dropdown No .html at the folder root; or a reserved name index/admin was used; or it wasn’t placed under templates/ in the program directory
Template selected but pages unchanged Your .html files were put into subdirectories (not loaded; must be at root level); or file names don’t match the base template so no override happened; or the page cache hasn’t expired
Structure is right but no styles, images broken Wrong asset paths. Must be /templates/<template-name>/css/style.css; relative paths are not allowed, and the leading / must not be omitted
An asset returns 403 Extension not in the whitelist (especially trying to access .html directly โ€” intentionally prohibited)
An asset returns 404 URL path doesn’t match the actual disk path (mind case sensitivity); or the file genuinely doesn’t exist
Multilingual subsite links lost the language prefix Detail links were hardcoded as /news/{{.ID}}. Must switch to {{.URL}}
Changed .html but the front end still shows the old version Templates are loaded at startup / when saving a site. After changing files, re-save the site info once in the admin to trigger a reload

โœ… Pre-Delivery Checklist

Check Standard
All .html at the folder root No .html in subdirectories
All assets use absolute paths Search once to confirm no relative patterns like href="css/ or src="images/
Template name matches asset paths If the folder is named mytheme, references must be /templates/mytheme/โ€ฆ
Detail links use {{.URL}} No hardcoded /news/{{.ID}} etc.
SEO variables wired up {{.PageTitle}} / {{.PageKeywords}} / {{.PageDescription}}
Header/footer reused Use {{template "header.html" .}} instead of copy-pasting across pages
No extraneous large files Remove design drafts, .psd, node_modules, etc. (they cannot be served anyway)
[โ† Multilingual Sites](18-multilang.html) [Image Upload Rules โ†’](20-upload.html)
This site uses cookies for essential functionality and to improve your experience. See our Privacy Policy.