go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/viewutil/render.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package viewutil
     9  
    10  import (
    11  	"bytes"
    12  	"html/template"
    13  )
    14  
    15  // Render compiles a template from text and renders it.
    16  func Render(templateText string, model any) (template.HTML, error) {
    17  	tmpl, err := template.New("").Parse(templateText)
    18  	if err != nil {
    19  		return "", err
    20  	}
    21  
    22  	buf := new(bytes.Buffer)
    23  	if err = tmpl.Execute(buf, model); err != nil {
    24  		return "", err
    25  	}
    26  	return template.HTML(buf.String()), nil
    27  }