github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/htmlrender/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/egonelbre/exp/htmlrender/dom"
     7  )
     8  
     9  var CustomTemplate = dom.MustTemplate(`
    10  	<div id="{{.id}}">
    11  		<span>{{.glyph}}</span>
    12  		<span>{{.title}}</span>
    13  		<span>{{ Render .child }}</span>
    14  	</div>
    15  `)
    16  
    17  type Input struct {
    18  	ID          string
    19  	Placeholder string
    20  }
    21  
    22  func (input Input) Render(w dom.Writer) {
    23  	w.Open("input")
    24  	if input.ID != "" {
    25  		w.Attr("id", input.ID)
    26  		w.Attr("name", input.ID)
    27  	}
    28  
    29  	dom.Class{"mdl-input"}.Render(w)
    30  
    31  	if input.Placeholder != "" {
    32  		w.Attr("placeholder", input.Placeholder)
    33  	}
    34  
    35  	w.Close("input")
    36  }
    37  
    38  func main() {
    39  	writer := dom.NewWriter()
    40  
    41  	writer.Render(dom.Form{
    42  		dom.Class{"example", "test"},
    43  		dom.Method{"POST"},
    44  
    45  		Input{"first", "First Name"},
    46  		Input{"last", "Last Name"},
    47  
    48  		CustomTemplate.Renderer(map[string]interface{}{
    49  			"id":    "clicky",
    50  			"glyph": "pen",
    51  			"title": "clicky",
    52  			"child": Input{"middle", "Middle Name"},
    53  		}),
    54  	})
    55  
    56  	fmt.Println(writer.String())
    57  }