0
我想定義代碼塊,如果它們被定義,它們將被注入基本模板。我不想將所有需要在一個頁面上的腳本包含到另一個不需要的腳本中。Golang&Martini代碼塊
我使用:
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
基本上什麼即時試圖做的是一樣的東西:
上的佈局:admin.tmpl
:
<script src="jquery.min.js"></script>
<script src="scripts.min.js"></script>
{{ footer_extra }}
和new.tmpl
:
{{define "footer_extra"}}
<!-- scripts just for this page -->
<script src="script-1.js"></script>
<script src="script-2.js"></script>
<script src="script-3.js"></script>
{{end}}
當我使用模板時,它似乎工作。
但我注意到我無法定義多個模板,這種模式會挫敗我試圖實現的目標。
index.tmpl
{{define "footer_extra"}}
<!-- scripts just for this page -->
<script src="script-1.js"></script>
<script src="script-2.js"></script>
{{end}}
new.tmpl
{{define "footer_extra"}}
<!-- scripts just for this page -->
<script src="script-3.js"></script>
<script src="script-4.js"></script>
{{end}}
layout.tmpl
<script src="main.js"></script>
{{template "footer_extra"}}
將拋出一個PANIC
template: redefinition of template "footer_extra"
請在你的代碼顯示的地方,你要訪問您的模板 –
看到這裏 - http://elithrar.github.io/article/approximating-html -template-inheritance/- 你可以在你的模板中通過「{{define XXX}}」來定義它們,但是讓它們爲空。 – elithrar