mustache icon indicating copy to clipboard operation
mustache copied to clipboard

Interface change suggestion

Open crantok opened this issue 9 years ago • 0 comments

This would mean that a literal template and a layout file (or vice versa) could be mixed in the same call. What do you think? If I implemented this would you merge it? The original API would still work.

In client code:

// Order of arguments is irrelevant
output := mustache.Render(
        mustache.Literal( "Here's my data: {{myData}}" ),
        mustache.LayoutFilename("./defaultlayout.mustache"),
        map[string]string{"myData":"Yes, this is my data"} )

In mustache.go:

type dataType int

const (
    uninitialisedDataType dataType = iota
    templateLiteral
    templateFilename
    layoutLiteral
    layoutFilename
)

type templateDescription struct {
    dataType
    data string
}

func Render(args ...interface{}) string {
    // If the first argument is a string then behave as original API
    // If using new API then only use the first occurrences of template and layout
    // ...
}

func Literal(template string) templateDescription {
    return templateDescription{templateLiteral, template}
}

func Filename(filename string) templateDescription {
    return templateDescription{templateFilename, filename}
}

func LayoutLiteral(template string) templateDescription {
    return templateDescription{layoutLiteral, template}
}

func LayoutFilename(filename string) templateDescription {
    return templateDescription{layoutFilename, filename}
}

crantok avatar Jan 15 '16 10:01 crantok