templates
将响应主体作为 template 文档执行。templates 提供用于构建简单动态页面的功能原语。功能包括 HTTP 子请求、HTML 文件包含、Markdown 渲染、JSON 解析、基本数据结构、随机性、时间等。
语法
templates [<matcher>] {
mime <types...>
between <open_delim> <close_delim>
root <path>
}
-
mime 是 templates 中间件将作用于的 MIME 类型;任何没有合格
Content-Type的响应都不会被作为模板评估。默认:
text/html text/plain。 -
between 是模板操作的起始和结束定界符。如果它们与文档的其余部分冲突,可以更改它们。
默认:
{{ }}。 -
root 是在使用访问文件系统的函数时的站点根目录。
默认为由
root指令设置的站点根目录;如果未设置,则为当前工作目录。
内置模板函数的文档可在 templates 模块 中找到。
示例
有关使用 templates 提供 Markdown 的完整站点示例,请查看 此网站 的源代码!具体查看 Caddyfile 和 src/docs/index.html。
为静态站点启用 templates:
example.com {
root * /srv
templates
file_server
}
要使用模板提供一个简单的静态响应,确保设置 Content-Type:
example.com {
header Content-Type text/plain
templates
respond `Current year is: {{now | date "2006"}}`
}