Golang 模板 - 使用模板变量作为范围循环内的全局变量

Posted

技术标签:

【中文标题】Golang 模板 - 使用模板变量作为范围循环内的全局变量【英文标题】:Golang Template - Use Template Variable As Global Variable From Within Range Loop 【发布时间】:2017-05-22 21:33:07 【问题描述】:

我会尽量简化。 我在 Golang 中有两个变量,它们被解析为模板文件。

这是我声明变量的 Golang 代码:

for _, issue := range issues 
    issueIDStr := strconv.Itoa(*issue.ID)
    parse[*issue.ID] = issueIDStr 
    parse[issueIDStr+"-label"] = "blah blah"

然后在我的 html 文件中:

 range .issues 
    <!-- Here I want to use the current issue's ID as a global variable which is outside the range loop -->
    <!-- According to the Golang doc which says the following:
             When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
         I can use $.Something to access a variable outside my range loop right...
         but how can I use a template variable as a global variable? I tried the following which doesn't work.
     $ID := .ID 
    <p> index $.$ID "author" </p>
 end 

运行此代码后,我收到错误:bad character U+0024 '$' 并引发恐慌。

我目前正在尝试的东西是完全不可能的,还是我缺少一些技巧?

谢谢:)

【问题讨论】:

你不能像 $.$ID 一样使用它,它不会像你预期的那样评估它。您可能必须以不同的方式构建它 你我使用全局地图并使用 ID 作为键 @Acidic,请添加显示您如何执行(...)模板的代码行。 【参考方案1】:

您应该简单地创建一个包含问题数组的 map[string]interface,然后将其用作模板执行数据。

然后您可以遍历问题并直接从模板访问其成员。

这是一个完整的小例子:

const t = `
 range .issues 
issue:  .ID 
    author:  .Author 
 end 
`

type Issue struct 
    ID     int
    Author string


func main() 
    issues := []Issue1, "Pepe", 2, "Cholo"
    data := map[string]interface"issues": issues
    tpl := template.Must(template.New("bla").Parse(t))
    tpl.Execute(os.Stdout, data)

哪些输出:

issue: 1
        author: Pepe

issue: 2
        author: Cholo

此外,如果您想要/需要添加特定于模板渲染过程的数据,您应该为此目的定义一个“丰富的”问题结构,并在将模型传递给模板执行之前对其进行转换。这可以针对静态已知的额外数据(作为 RichIssue 的简单成员)和动态加载的数据(作为地图的键/值)完成。

这是一个显示上述建议的扩展示例:

const t = `
 range .issues 
issue:  .ID 
    author:  .Author 
    static1:  .Static1 
    dyn1:  .Data.dyn1 
 end 
`

type Issue struct 
    ID     int
    Author string


type RichIssue struct 
    Issue
    Static1 string                 // some statically known additional data for rendering
    Data    map[string]interface // container for dynamic data (if needed)


func GetIssueStatic1(i Issue) string 
    return strconv.Itoa(i.ID) + i.Author // whatever


func GetIssueDyn1(i Issue) string 
    return strconv.Itoa(len(i.Author)) // whatever


func EnrichIssue(issue Issue) RichIssue 
    return RichIssue
        Issue:   issue,
        Static1: GetIssueStatic1(issue),
        // in this contrived example I build "dynamic" members from static
        // hardcoded strings but these fields (names and data) should come from
        // some kind of configuration or query result to be actually dynamic
        // (and justify being set in a map instead of being simple static
        // members as Static1)
        Data: map[string]interface
            "dyn1": GetIssueDyn1(issue),
            "dyn2": 2,
            "dyn3": "blabla",
        ,
    


func EnrichIssues(issues []Issue) []RichIssue 
    r := make([]RichIssue, len(issues))
    for i, issue := range issues 
        r[i] = EnrichIssue(issue)
    
    return r


func main() 
    issues := []Issue1, "Pepe", 2, "Cholo"
    data := map[string]interface"issues": EnrichIssues(issues)
    tpl := template.Must(template.New("bla").Parse(t))
    tpl.Execute(os.Stdout, data)

产生以下输出:

issue: 1
    author: Pepe
    static1: 1Pepe
    dyn1: 4

issue: 2
    author: Cholo
    static1: 2Cholo
    dyn1: 5

【讨论】:

是的,我已经设法做到了,但我的问题是,我需要为 []issue 中的每个问题添加一些数据,但我无法更改问题结构类型。因此,例如,您有 issue.author = "Pepe",如果我需要为每个问题添加一条新信息怎么办,这就是我遇到的麻烦。 如果您需要为每个项目(问题)添加静态已知数据,最惯用和最直接的方法是简单地定义一个新问题结构,例如“RichIssue”,它可以包含您想要的所有数据喜欢暴露给模板,然后将 []Issue 转换为 []RichIssue 并使用结果来针对它执行模板。 如果您需要添加动态数据,您应该向 RichIssue 添加一个 map[string]interface 成员,这样当您将问题转换为 RichIssue 时,您可以向该成员添加额外的数据(我会尝试以避免所有静态已知数据都被公开)。 @Acidic 你有机会看到我更新的答案吗? 是的,我看到了,但这并不是我想要的答案。我的问题真的很复杂,我很确定我正在尝试的事情是不可能的。我设法找到了另一种解决方案,但我很欣赏这个答案。 :)

以上是关于Golang 模板 - 使用模板变量作为范围循环内的全局变量的主要内容,如果未能解决你的问题,请参考以下文章

Django 使用模板变量的值作为另一个变量名的一部分

范围内的转义模板

如何在 Django 2.1 中的模板内的 for 循环内设置变量?

helm charts 模板编程

如何在 Django 模板语言中正确构建 for 循环

通过 Golang 中的模板解析自定义变量