我想让类似于UI组件的东西在GO模板中100%可重用,但我不知道是否可能做到这一点.所以我试着做下一件事:
{{define "components/menu-button"}}
<a href="{{.link}}" class="text-white">{{.content}}</a>
{{end}}
这是我的组件,它应该是map
,因为属性都是小写的.
然后在我的主页上,我有一个小菜单,它使用了导航栏中components/menu-button
个组件的3倍:
<div class="hidden gap-4 sm:flex">
{{template "components/menu-button" {"link": "/contact", "content": "Contact"}}}
{{template "components/menu-button" {"link": "/docs", "content": "Docs"}}}
{{template "components/menu-button" {"link": "/download", "content": "Download"}}}
</div>
但我不知道我是否能以某种方式创建一个map
,就像我在示例中所做的那样,这类似于JSON的事情,但我try 了.
顺便说一句,它给了我下一个错误:
unexpected "{" in template clause