๐งฉ Common Tags (Utility Helpers)
Last updated๏ผ2026-11-25
๐งฉ Common Tags (General Helper Tags)
This chapter collects the common tags and functions (available to every front-end template) โ not limited to any content type. Gitl.cn CMS is built on Go’s standard html/template engine, providing shared capabilities in two forms: “template functions + global variables”.
๐ Common Tag Capabilities Overview
The following capabilities are usable in all front-end templates, ordered by frequency of use.
| Capability | Usage | Description |
|---|---|---|
| Template partial reuse | {{template "header.html" .}} |
Reuse common fragments with Go named templates (see “Template Nesting”) |
| Date formatting | {{.CreatedAt \| formatDate "Y-m-d"}} |
Supports Y-m-d / Y/m/d / Yๅนดmๆdๆฅ (date only) |
| Content truncation | {{.Summary \| truncate 50}} |
Truncates by character (rune), CJK-friendly, appends ... when over |
| Output HTML as-is | {{.Content \| safeHTML}} |
Rich-text fields are escaped by default; marked as safe HTML they render as-is |
| Numeric addition | {{add .CurrentPage 1}} |
Integer addition, for indices and offsets |
| JSON/multi-value splitting | {{range splitJSON .Images}} |
Parses multi-value fields like multi-images and tags into rangeable slices |
| Custom fragments | {{.FooterNotice}} |
Admin “template content fragments” injected as same-named global variables |
| Breadcrumbs | .ParentCategory / .CategoryName |
Section pages carry parent and current section data |
| Current URL | {{.SiteDomain}} / {{.URL}} |
Site domain / content detail link (the form of {{.URL}} is governed by admin “Configuration โ URL rules”; see “Output Controlled by Admin Configuration” below) |
| Sitemap and SEO files | /sitemap.xml, /llms.txt, etc. |
Generated automatically at runtime, auto-sharded for large data |
| Image display size | Controlled by CSS | Original image unchanged; display size determined by CSS |
๐ง Common Template Functions
The following functions can be called from any front-end template, with syntax {{pipe | function arg}} or {{function arg}}.
๐ formatDate โ Date Formatting
| format Value | Output Example | Notes |
|---|---|---|
Y-m-d |
2026-08-19 | Default, dash-separated |
Y/m/d |
2026/08/19 | Slash-separated |
Yๅนดmๆdๆฅ |
2026ๅนด08ๆ19ๆฅ | Chinese format |
<time>{{.CreatedAt | formatDate "Y-m-d"}}</time>
<!-- List item โ -->
{{range .Posts}}
<span>{{.CreatedAt | formatDate "Yๅนดmๆdๆฅ"}}</span>
{{end}}
Processes only the date part; returns empty when the field is empty / non-string / too short (nil-safe, never a 500). If the string is long enough but doesn’t match one of the three formats above, it is returned as-is (also no 500). For “hours:minutes:seconds”, output .CreatedAt as-is.
โ๏ธ truncate โ Content Truncation (CJK-Friendly)
Truncates by character count (rune), avoiding uneven CJK/Latin lengths; appends ... at the end when over.
{{.Summary | truncate 50}}
<!-- Truncate product description to 30 characters -->
<p>{{.Product.Description | truncate 30}}</p>
๐งฑ safeHTML โ Output HTML As-Is
If a field contains HTML (e.g. rich text from the admin panel), it’s escaped by default; use safeHTML to render it as-is (output after being marked safe HTML).
<div class="rich">{{.Content | safeHTML}}</div>
๐ add โ Numeric Addition
Adds integers, useful for indices, offsets, etc.
<span>Page {{add .CurrentPage 1}}</span>
๐ฃ splitJSON โ JSON/Multi-Value Array to List
Parses JSON array strings stored in the database (e.g. multi-image .Images) into slices you can range; splits by lines when not JSON.
{{range splitJSON .Images}}
<img src="{{.}}" alt="">
{{end}}
๐งฉ Template Nesting (Partial Reuse)
Gitl.cn CMS uses Go named templates for common fragment reuse: write shared fragments as {{define "name"}}โฆ{{end}} and include them where needed with {{template "name" .}}, where . is the data root passed in.
<!-- header.html at top -->
{{template "header.html" .}}
<!-- Footer -->
{{template "footer.html" .}}
<!-- Built-in icon fragment (defined in the default template)-->
<div class="icon">{{template "icon-arrow-right"}}</div>
Tip: header.html, footer.html, and similar files under templates// are common partials that any page can include.
๐ Global Variables (Readable Directly on Any Page)
Besides per-chapter fields, the following variables are available in front-end templates. Note: CanonicalURL / PageJSONLD / BreadcrumbJSONLD are injected only on detail pages / section pages (empty on home, list, and search pages); the rest are site-wide.
| Variable | Meaning |
|---|---|
{{.SiteDomain}} |
Site domain (with protocol, e.g. https://www.gitl.cn) |
{{.SiteTitle}} / {{.SiteSubtitle}} |
Site title / subtitle |
{{.SitePrefix}} |
Multi-language path prefix (empty for the default site, /en for the English site) |
{{.SiteHomeURL}} / {{.SiteContactURL}} / {{.SiteAboutURL}} |
Home / Contact Us / About Us links |
{{.CompanyName}} / {{.CompanyPhone}} / {{.CompanyEmail}} โฆ |
Company info (see the “Company Info” chapter) |
{{.Navigations}} / {{.TopCategories}} / {{.ProductCategories}} |
Navigation menus / top-level sections / product sections |
{{.CurrentYear}} |
Current year (commonly used in footer copyright) |
{{.LangSwitcher}} |
Multi-language switcher data (on multi-sites) |
{{.CanonicalURL}} |
Current page’s absolute canonical address (available on detail / section pages; empty on home and list pages) |
{{.OGLocale}} |
Open Graph locale (e.g. zh_CN / en_US / ja_JP), used for og:locale NEW |
{{.OGImage}} |
Open Graph share image (absolute URL, domain auto-completed by the system; falls back to the site default image when there’s no body image) NEW |
{{.PageJSONLD}} |
Detail page structured data (Product / Article, already safeHTML; detail pages only) NEW |
{{.BreadcrumbJSONLD}} |
Breadcrumb structured data (BreadcrumbList, already safeHTML; section pages / detail pages only) NEW |
<a href="{{.SiteDomain}}">{{.SiteTitle}}</a>
<p>ยฉ {{.CurrentYear}} {{.CompanyName}}</p>
<a href="{{.SiteContactURL}}">่็ณปๆไปฌ</a>
๐ SEO Head Tags (canonical / hreflang / OG / JSON-LD)
Below is the SEO head markup actually used in production templates; apply it directly to your own header.html / footer.html. All variables are injected automatically by the Go backend โ no hand-written URLs needed.
๐ canonical / hreflang / OG in
<!-- canonical: absolute address, automatically carries the site prefix (/en etc.) -->
<link rel="canonical" href="{{.CanonicalURL}}">
<!-- hreflang: multi-language cross-links (non-empty only on multi-sites) -->
{{range .HrefLangs}}<link rel="alternate" hreflang="{{.Lang}}" href="{{.Href}}">
{{end}}
<!-- Open Graph -->
<meta property="og:type" content="website">
<meta property="og:url" content="{{.CanonicalURL}}">
<meta property="og:locale" content="{{.OGLocale}}">
<meta property="og:image" content="{{.OGImage}}">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
Notes: {{.CanonicalURL}} is already absolute and automatically includes the multi-language prefix; {{.OGLocale}} outputs zh_CN / en_US etc.; {{.OGImage}} is already an absolute URL (the admin panel’s “site OG image” or a default placeholder) โ don’t prepend the domain again.
๐งฑ Detail Page Structured Data (Output Before in footer)
<!-- Place before </body> in footer.html -->
{{if .PageJSONLD}}{{.PageJSONLD}}{{end}}
{{if .BreadcrumbJSONLD}}{{.BreadcrumbJSONLD}}{{end}}
{{.PageJSONLD}} automatically generates Product or Article type JSON-LD on post / product / case / single-page detail pages; {{.BreadcrumbJSONLD}} generates BreadcrumbList. Both are already safeHTML โ **just output them directly; do not add safeHTML again, and do not manually wrap them in `
CloudSite SaaS