duanchuaiwan0063 2016-08-31 16:41
浏览 343

嵌套golang模板中的变量

I want to define variables inside of a golang template, instead of in a binary so that there is no need to recompile.

In Go, I set some vars:

var animals = map[string]string{
    "spirit_animal":    "cat",
    "spirit_predator":  "dog",
}

I execute the template with this: t.ExecuteTemplate(w, "main", variables) which passes these vars to the template.

Now I would like to take these vars from go and into the "main" template.

{{$spirit_animal:="cat"}} {{$spirit_animal}}

And I call sub-templates like this:

{{ template "navbar" . }}

The problem is that nested templates (sub templates) do not appear to have access to any variables.

From the documentation, "A template invocation does not inherit variables from the point of its invocation." Reading the Documentation for "text/template", it sounds like variables may not be able to be used in nested templates.

Any suggestions on how to get these vars out of a binary and into a single text location accessible by nested templates that doesn't need to be recompiled on change?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 请问这个是什么意思?
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样