0

これをできるだけ簡単にしようと思います。Golang には、テンプレート ファイルに解析される 2 つの変数があります。

変数を宣言する 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 '$'パニックがスローされます。

私が現在試みていることは完全に不可能ですか、それとも私が見逃しているトリックはありますか?

ありがとう :)

4

1 に答える 1

1

問題配列を含む map[string]interface{} を作成し、それをテンプレート実行データとして使用するだけです。

次に、問題をループして、テンプレートからそのメンバーに直接アクセスできます。

ここに小さな完全な例があります:

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

type Issue struct {
    ID     int
    Author string
}

func main() {
    issues := []Issue{{1, "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

さらに、テンプレート レンダリング プロセスに固有のデータを追加する必要がある場合は、この目的のために「豊富な」Issue 構造体を定義し、テンプレートの実行に渡す前にモデルを変換する必要があります。これは、静的に既知の追加データ (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 := []Issue{{1, "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
于 2017-01-08T23:00:40.333 に答える