Git Product home page Git Product logo

htmlbuilder-go's Introduction

Building HTML from Go code

Usage

package main

import (
	"fmt"

	hb "github.com/wayan/htmlbuilder-go"
)

func main() {
	h := hb.NewHtml()

	// adds element with attributes (as key, value, pairs)
	div := h.El("div", "class", "hidden", "lang", "en")

	// div is open, can add attributes (conditionally)
	div.Attr("id", "first-id").Text("Special")
	// adding nested element
	div.El("h1").Text("Just a header")

	// adding another element to top level, closes all nested elements
	h.El("p").Text("Short paragraph")

	// closing all elements
	h.Close()

	// following Println yields
	// <div class="hidden" lang="en" id="first-id">Special<h1>Just a header</h1></div><p>Short paragraph</p>
	fmt.Println(h.String())

	// another example using .C (content which closes current element)
	buildHead := func(h *hb.Html) {
		h.ElSingle("meta", "charset", "utf-8")
		h.El("title").Text("Title")
	}

	buildBody := func(h *hb.Html) {
		d := h.El("div", "id", "div-one")
		d.El("h1").Text("Head")
		// nested paragraph with selected attribute
		d.El("p", "selected").Text("Some paragraph")
	}

	// following Println yields
	// <html><head><meta charset="utf-8"><title>Title</title></head><body><div id="div-one"><h1>Head</h1><p selected>Some paragraph</p></div></body></html>
	fmt.Println(
		hb.NewHtml().El("html").El("head").C(buildHead).El("body").C(buildBody).Close().String(),
	)

}

htmlbuilder-go's People

Contributors

wayan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.