Skip to content

模板变量参考

This content is not available in your language yet.

本页是模板变量的快速参考。完整详细版本请查阅 API 参考 > 模板变量表

以下变量在所有页面模板中可用:

变量类型说明
config.domainstring站点域名(含协议头,无尾部斜杠)
config.siteNamestring站点名称
config.siteDescriptionstring站点描述
config.avatarstring头像路径
config.logostringLogo 路径
theme_configObject主题自定义配置(来自 config.json 的 customConfig)
menus[]Menu导航菜单列表
tags[]Tag所有标签列表
nowtime.Time当前时间
字段类型说明
post.titlestring文章标题
post.contentstring渲染后的 HTML 内容
post.datestring发布日期(已格式化的字符串)
post.dateFormatstring格式化后的日期显示
post.linkstring文章 URL 路径
post.tags[]Tag文章标签列表
post.featurestring特色图片 URL(无图时为空字符串)
post.isTopbool是否置顶
post.hideInListbool是否在列表中隐藏
错误写法正确写法说明
post.urlpost.link字段名为 link
post.imagepost.feature特色图片字段名为 feature
post.pinnedpost.isTop置顶字段名为 isTop
post.created_atpost.date日期字段名为 date
config.titleconfig.siteName站点名称字段为 siteName
config.urlconfig.domain域名字段为 domain
tag.slugtag.link标签链接字段为 link
tag.posts_counttag.count文章计数字段为 count
pagination.previouspagination.prev上一页简写为 prev
页面模板专属变量
index.htmlposts, pagination
post.htmlpost(单个对象)
archives.htmlposts
tag.htmlposts, tag, current_tag
tags.html仅全局变量
blog.htmlposts, pagination
memos.htmlmemos
404.html仅 config, theme_config, menus, now
操作Jinja2 (Pongo2)Go TemplatesEJS
输出变量{{ config.siteName }}{{ .Config.SiteName }}<%= config.siteName %>
输出 HTML{{ post.content|safe }}{{ .Post.Content }}<%- post.content %>
条件{% if x %}...{% endif %}{{ if .X }}...{{ end }}<% if (x) { %>...<% } %>
循环{% for p in posts %}{{ range .Posts }}<% posts.forEach(function(p) { %>
引入{% include "partials/x.html" %}{{ template "x" . }}<%- include('partials/x') %>

详细的三引擎语法差异请查阅各引擎专属指南:Jinja2Go TemplatesEJS