templates
將回應 body 作為 template 文件執行。 Templates 提供了用於建立簡單動態頁面的功能性基本元素。功能包括 HTTP 子請求、HTML 檔案包含、Markdown 渲染、JSON 解析、基本資料結構、隨機性、時間等等。
語法
templates [<matcher>] {
mime <types...>
between <open_delim> <close_delim>
root <path>
}
-
mime 是 templates 中介軟體將作用的 MIME 類型;任何不具有符合條件的
Content-Type
的回應將不會被評估為 templates。預設值:
text/html text/plain
。 -
between 是 template 動作的開頭和結尾分隔符。如果它們干擾到文件的其餘部分,您可以更改它們。
預設值:
{{ }}
。 -
root 是網站根目錄,當使用訪問檔案系統的函數時。
預設為
root
指令設定的網站根目錄,如果未設定,則為目前的工作目錄。
內建 template 函數的文件可以在 templates 模組 中找到。
範例
如需使用 templates 提供 markdown 的完整網站範例,請查看 這個網站 的原始碼!具體來說,請查看 Caddyfile
和 src/docs/index.html
。
為靜態網站啟用 templates
example.com {
root * /srv
templates
file_server
}
若要使用 template 提供簡單的靜態回應,請務必設定 Content-Type
example.com {
header Content-Type text/plain
templates
respond `Current year is: {{now | date "2006"}}`
}