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

     1  package dom
     2  
     3  import "strings"
     4  
     5  type Div []Renderer
     6  
     7  func (el Div) Render(w Writer) {
     8  	el.RenderOpen(w)
     9  	el.RenderClose(w)
    10  }
    11  
    12  func (el Div) RenderOpen(w Writer) {
    13  	w.Open("div")
    14  	w.RenderAll(el...)
    15  }
    16  
    17  func (el Div) RenderClose(w Writer) {
    18  	w.Close("div")
    19  }
    20  
    21  type Span []Renderer
    22  
    23  func (el Span) Render(w Writer) {
    24  	el.RenderOpen(w)
    25  	el.RenderClose(w)
    26  }
    27  
    28  func (el Span) RenderOpen(w Writer) {
    29  	w.Open("div")
    30  	w.RenderAll(el...)
    31  }
    32  
    33  func (el Span) RenderClose(w Writer) {
    34  	w.Close("div")
    35  }
    36  
    37  type Text struct{ Content string }
    38  
    39  func (text Text) Render(w Writer) { w.Text(text.Content) }
    40  
    41  type Attrs map[string]string
    42  
    43  func (attrs Attrs) Render(w Writer) {
    44  	for name, value := range attrs {
    45  		w.Attr(name, value)
    46  	}
    47  }
    48  
    49  type Attr struct{ Name, Value string }
    50  
    51  func (attr Attr) Render(w Writer) {
    52  	w.Attr(attr.Name, attr.Value)
    53  }
    54  
    55  type Children []Renderer
    56  
    57  func (children Children) Render(w Writer) {
    58  	for _, child := range children {
    59  		child.Render(w)
    60  	}
    61  }
    62  
    63  type URL struct{ Value string }
    64  
    65  func (attr URL) Render(w Writer) { w.Attr("href", attr.Value) }
    66  
    67  type Class []string
    68  
    69  func (class Class) Render(w Writer) {
    70  	w.Attr("class", strings.Join([]string(class), " "))
    71  }