-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package mustacheutil | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/cbroglie/mustache" | ||
) | ||
|
||
type MustacheSet struct { | ||
Filenames map[string]string | ||
Templates map[string]*mustache.Template | ||
} | ||
|
||
func (ms *MustacheSet) ReadTemplates() error { | ||
for key, filename := range ms.Filenames { | ||
if filename == "" { | ||
continue | ||
} | ||
if tmpl, err := mustache.ParseFile(filename); err != nil { | ||
return err | ||
} else { | ||
if ms.Templates == nil { | ||
ms.Templates = map[string]*mustache.Template{} | ||
} | ||
ms.Templates[key] = tmpl | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (ms *MustacheSet) RenderTemplate(key string, data map[string]string) (*bytes.Buffer, error) { | ||
tmpl, ok := ms.Templates[key] | ||
if !ok { | ||
return nil, fmt.Errorf("template key not present for key (%s)", key) | ||
} else if tmpl == nil { | ||
return nil, fmt.Errorf("template is nil for key (%s)", key) | ||
} else { | ||
var buf bytes.Buffer | ||
if err := tmpl.FRender(&buf, data); err != nil { | ||
return nil, err | ||
} else { | ||
return &buf, nil | ||
} | ||
} | ||
} |