2 回答

TA貢獻(xiàn)1911條經(jīng)驗 獲得超7個贊
我認(rèn)為您嘗試使用嵌套模板的方法是錯誤的。如果你想.在嵌套模板中定義,你必須為嵌套模板的調(diào)用提供一個參數(shù),就像你對ExecuteTemplate函數(shù)所做的一樣:
{{define "RT"}}
Status - {{.Status}}
Proto - {{.Proto}}
{{end}}
{{define "HT"}}
{{range $key, $val := .}}
{{$key}} - {{$val}}
{{end}}
{{end}}
{{define "BT"}}
{{.}}
{{end}}
{{define "MT"}}
<html>
<body>
<pre>
-- Response --
{{template "RT" .Resp}}
-- Header --
{{template "HT" .Header}}
-- Body --
{{template "BT" .Body}}
</pre>
</body>
</html>
{{end}}
{{template "MT"}}
您似乎錯過的重要部分是模板不封裝狀態(tài)。當(dāng)您執(zhí)行模板時,引擎會評估給定參數(shù)的模板,然后寫出生成的文本。它不會保存參數(shù)或為將來調(diào)用而生成的任何內(nèi)容。
文檔
文檔的相關(guān)部分:
行動
這是操作列表?!皡?shù)”和“管道”是對數(shù)據(jù)的評估,詳細(xì)定義如下。
...
{{template "name"}}
The template with the specified name is executed with nil data.
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.
...
我希望這可以解決問題。
- 2 回答
- 0 關(guān)注
- 242 瀏覽
添加回答
舉報