Switch or if/elseif/else inside golang HTML templates

后端 未结 3 802
攒了一身酷
攒了一身酷 2020-12-24 01:18

I have this struct :

const (
    paragraph_hypothesis = 1<

        
相关标签:
3条回答
  • 2020-12-24 01:54

    Templates are logic-less. They're not supposed to have this kind of logic. The maximum logic you can have is a bunch of if.

    In such a case, you're supposed to do it like this:

    {{if .IsAttachment}}
        -- attachment presentation code --
    {{end}}
    
    {{if .IsMenu}}
        -- menu --
    {{end}}
    
    {{if .IsDefault}}
        -- default code --
    {{end}}
    
    0 讨论(0)
  • 2020-12-24 01:54

    Yes, you can use {{else if .IsMenu}}

    0 讨论(0)
  • 2020-12-24 01:56

    You can achieve switch functionality by adding custom functions to the template.FuncMap.

    In the example below I've defined a function, printPara (paratype int) string which takes one of your defined paragraph types and changes it's output accordingly.

    Please note that, in the actual template, the .Paratype is piped into the printpara function. This is how to pass parameters in templates. Please note that there are restrictions on the number and form of the output parameters for functions added to FuncMaps. This page has some good info, as well as the first link.

    package main
    
    import (
        "fmt"
        "os"
        "html/template"
    )
    
    func main() {
    
        const (
            paragraph_hypothesis = 1 << iota
            paragraph_attachment = 1 << iota
            paragraph_menu       = 1 << iota
        )
    
        const text = "{{.Paratype | printpara}}\n" // A simple test template
    
        type Paragraph struct {
            Paratype int
        }
    
        var paralist = []*Paragraph{
            &Paragraph{paragraph_hypothesis},
            &Paragraph{paragraph_attachment},
            &Paragraph{paragraph_menu},
        }
    
        t := template.New("testparagraphs")
    
        printPara := func(paratype int) string {
            text := ""
            switch paratype {
            case paragraph_hypothesis:
                text = "This is a hypothesis\n"
            case paragraph_attachment:
                text = "This is an attachment\n"
            case paragraph_menu:
                text = "Menu\n1:\n2:\n3:\n\nPick any option:\n"
            }
            return text
        }
    
        template.Must(t.Funcs(template.FuncMap{"printpara": printPara}).Parse(text))
    
        for _, p := range paralist {
            err := t.Execute(os.Stdout, p)
            if err != nil {
                fmt.Println("executing template:", err)
            }
        }
    }
    

    Produces:

    This is a hypothesis

    This is an attachment

    Menu
    1:
    2:
    3:

    Pick any option:

    Playground link

    Hope that helps, I'm pretty sure the code could be cleaned up a bit, but I've tried to stay close to the example code you provided.

    0 讨论(0)
提交回复
热议问题