raymond.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Package raymond provides handlebars evaluation
  2. package raymond
  3. import "github.com/sirupsen/logrus"
  4. var log *logrus.Entry
  5. func init() {
  6. log = logrus.NewEntry(logrus.StandardLogger())
  7. }
  8. // SetLogger allows the user to set a customer logger adding the ability to add custom fields to
  9. // the log entries.
  10. func SetLogger(entry *logrus.Entry) {
  11. log = entry
  12. }
  13. // Render parses a template and evaluates it with given context
  14. //
  15. // Note that this function call is not optimal as your template is parsed everytime you call it. You should use Parse() function instead.
  16. func Render(source string, ctx interface{}) (string, error) {
  17. // parse template
  18. tpl, err := Parse(source)
  19. if err != nil {
  20. return "", err
  21. }
  22. // renders template
  23. str, err := tpl.Exec(ctx)
  24. if err != nil {
  25. return "", err
  26. }
  27. return str, nil
  28. }
  29. // MustRender parses a template and evaluates it with given context. It panics on error.
  30. //
  31. // Note that this function call is not optimal as your template is parsed everytime you call it. You should use Parse() function instead.
  32. func MustRender(source string, ctx interface{}) string {
  33. return MustParse(source).MustExec(ctx)
  34. }