All Projects → theplant → htmlgo

theplant / htmlgo

Licence: MIT License
Type safe and modularize way to generate html on server side.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to htmlgo

Git-API
Gets info from github and transfers into json styled data
Stars: ✭ 18 (-58.14%)
Mutual labels:  module
DebugPx
PowerShell Debugging Toolkit (feat. the breakpoint and ifdebug commands)
Stars: ✭ 41 (-4.65%)
Mutual labels:  module
terraform-aws-elasticache
Terraform module to create Elasticache Cluster and replica for Redis and Memcache.
Stars: ✭ 19 (-55.81%)
Mutual labels:  module
Alter-Entity-Autocomplete
Drupal 8 module to alter Entity Autocomplete suggestion list.
Stars: ✭ 38 (-11.63%)
Mutual labels:  module
yii2-logreader
Yii2 Log Reader
Stars: ✭ 31 (-27.91%)
Mutual labels:  module
JlContentFieldsFilter
Модуль фильтрации материалов Joomla по дополнительным полям
Stars: ✭ 20 (-53.49%)
Mutual labels:  module
ejabberd mod apns
An ejabberd module to send PUSH messages to iOS devices through APNS
Stars: ✭ 31 (-27.91%)
Mutual labels:  module
ci4-album
🔥 CodeIgniter 4 example Album module uses Domain Driven Design Architecture with Tactical Pattern
Stars: ✭ 67 (+55.81%)
Mutual labels:  module
Starter-Module-PrestaShop-1.6
A boilerplate that will get you started with your module for PrestaShop v1.6
Stars: ✭ 24 (-44.19%)
Mutual labels:  module
vbl
A collection of useful Bash modules to simplify everyday programming
Stars: ✭ 15 (-65.12%)
Mutual labels:  module
java9-module-examples
a list of Java 9 module samples to dive into the modular world
Stars: ✭ 25 (-41.86%)
Mutual labels:  module
gdsmod
Godot Module Replayer
Stars: ✭ 32 (-25.58%)
Mutual labels:  module
vania
A module which fairly distributes a list of arbitrary objects among a set of targets, considering weights.
Stars: ✭ 75 (+74.42%)
Mutual labels:  module
FormatPx
Better Formatting in PowerShell
Stars: ✭ 63 (+46.51%)
Mutual labels:  module
Moduler
Android Moduler 组件化demo
Stars: ✭ 33 (-23.26%)
Mutual labels:  module
polybar-dwm-module
A dwm module for polybar
Stars: ✭ 91 (+111.63%)
Mutual labels:  module
core
augejs is a progressive Node.js framework for building applications. https://github.com/augejs/augejs.github.io
Stars: ✭ 18 (-58.14%)
Mutual labels:  module
webtrees-pedigree-chart
SVG based pedigree chart module for webtrees genealogy application.
Stars: ✭ 24 (-44.19%)
Mutual labels:  module
QtApng
An apng image plugin for Qt to support animated PNGs
Stars: ✭ 75 (+74.42%)
Mutual labels:  module
surge
Network toolbox Surge rules, modules and configuration files, For pure self use.
Stars: ✭ 207 (+381.4%)
Mutual labels:  module

htmlgo

Type safe and modularize way to generate html on server side. Download the package with go get -v github.com/theplant/htmlgo and import the package with . gives you simpler code:

import (
	. "github.com/theplant/htmlgo"
)

also checkout full API documentation at: https://godoc.org/github.com/theplant/htmlgo

Create a simple div, Text will be escaped by html

	banner := "We write html in Go"
	comp := Div(
	    Text("123<h1>"),
	    Textf("Hello, %s", banner),
	    Br(),
	)
	Fprint(os.Stdout, comp, context.TODO())
	//Output:
	// <div>123&lt;h1&gt;Hello, We write html in Go
	// <br></div>

Create a full html page

	comp := HTML(
	    Head(
	        Meta().Charset("utf8"),
	        Title("My test page"),
	    ),
	    Body(
	        Img("images/firefox-icon.png").Alt("My test image"),
	    ),
	)
	Fprint(os.Stdout, comp, context.TODO())
	//Output:
	// <!DOCTYPE html>
	//
	// <html>
	// <head>
	// <meta charset='utf8'></meta>
	//
	// <title>My test page</title>
	// </head>
	//
	// <body>
	// <img src='images/firefox-icon.png' alt='My test image'></img>
	// </body>
	// </html>

Use RawHTML and Component

	userProfile := func(username string, avatarURL string) HTMLComponent {
	    return ComponentFunc(func(ctx context.Context) (r []byte, err error) {
	        return Div(
	            H1(username).Class("profileName"),
	            Img(avatarURL).Class("profileImage"),
	            RawHTML("<svg>complicated svg</svg>\n"),
	        ).Class("userProfile").MarshalHTML(ctx)
	    })
	}
	
	comp := Ul(
	    Li(
	        userProfile("felix<h1>", "http://image.com/img1.png"),
	    ),
	    Li(
	        userProfile("john", "http://image.com/img2.png"),
	    ),
	)
	Fprint(os.Stdout, comp, context.TODO())
	//Output:
	// <ul>
	// <li>
	// <div class='userProfile'>
	// <h1 class='profileName'>felix&lt;h1&gt;</h1>
	//
	// <img src='http://image.com/img1.png' class='profileImage'></img>
	// <svg>complicated svg</svg>
	// </div>
	// </li>
	//
	// <li>
	// <div class='userProfile'>
	// <h1 class='profileName'>john</h1>
	//
	// <img src='http://image.com/img2.png' class='profileImage'></img>
	// <svg>complicated svg</svg>
	// </div>
	// </li>
	// </ul>

More complicated customized component

	/*
	    Define MySelect as follows:
	
	    type MySelectBuilder struct {
	        options  [][]string
	        selected string
	    }
	
	    func MySelect() *MySelectBuilder {
	        return &MySelectBuilder{}
	    }
	
	    func (b *MySelectBuilder) Options(opts [][]string) (r *MySelectBuilder) {
	        b.options = opts
	        return b
	    }
	
	    func (b *MySelectBuilder) Selected(selected string) (r *MySelectBuilder) {
	        b.selected = selected
	        return b
	    }
	
	    func (b *MySelectBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
	        opts := []HTMLComponent{}
	        for _, op := range b.options {
	            var opt HTMLComponent
	            if op[0] == b.selected {
	                opt = Option(op[1]).Value(op[0]).Attr("selected", "true")
	            } else {
	                opt = Option(op[1]).Value(op[0])
	            }
	            opts = append(opts, opt)
	        }
	        return Select(opts...).MarshalHTML(ctx)
	    }
	*/
	
	comp := MySelect().Options([][]string{
	    {"1", "label 1"},
	    {"2", "label 2"},
	    {"3", "label 3"},
	}).Selected("2")
	
	Fprint(os.Stdout, comp, context.TODO())
	//Output:
	// <select>
	// <option value='1'>label 1</option>
	//
	// <option value='2' selected='true'>label 2</option>
	//
	// <option value='3'>label 3</option>
	// </select>

Write a little bit of JavaScript and stylesheet

	comp := Div(
	    Button("Hello").Id("hello"),
	    Style(`
	.container {
	    background-color: red;
	}
	`),
	
	    Script(`
	var b = document.getElementById("hello")
	b.onclick = function(e){
	    alert("Hello");
	}
	`),
	).Class("container")
	
	Fprint(os.Stdout, comp, context.TODO())
	//Output:
	// <div class='container'>
	// <button id='hello'>Hello</button>
	//
	// <style type='text/css'>
	// 	.container {
	// 		background-color: red;
	// 	}
	// </style>
	//
	// <script type='text/javascript'>
	// 	var b = document.getElementById("hello")
	// 	b.onclick = function(e){
	// 		alert("Hello");
	// 	}
	// </script>
	// </div>

An example about how to integrate into http.Handler, and how to do layout, and how to use context.

	type User struct {
	    Name string
	}
	
	userStatus := func() HTMLComponent {
	    return ComponentFunc(func(ctx context.Context) (r []byte, err error) {
	
	        if currentUser, ok := ctx.Value("currentUser").(*User); ok {
	            return Div(
	                Text(currentUser.Name),
	            ).Class("username").MarshalHTML(ctx)
	        }
	
	        return Div(Text("Login")).Class("login").MarshalHTML(ctx)
	    })
	}
	
	myHeader := func() HTMLComponent {
	    return Div(
	        Text("header"),
	        userStatus(),
	    ).Class("header")
	}
	myFooter := func() HTMLComponent {
	    return Div(Text("footer")).Class("footer")
	}
	
	layout := func(in HTMLComponent) (out HTMLComponent) {
	    out = HTML(
	        Head(
	            Meta().Charset("utf8"),
	        ),
	        Body(
	            myHeader(),
	            in,
	            myFooter(),
	        ),
	    )
	    return
	}
	
	getLoginUserFromCookie := func(r *http.Request) *User {
	    return &User{Name: "felix"}
	}
	
	homeHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	    user := getLoginUserFromCookie(r)
	    ctx := context.WithValue(context.TODO(), "currentUser", user)
	
	    root := Div(
	        Text("This is my home page"),
	    )
	
	    Fprint(w, layout(root), ctx)
	})
	
	w := httptest.NewRecorder()
	r := httptest.NewRequest("GET", "/", nil)
	homeHandler.ServeHTTP(w, r)
	
	fmt.Println(w.Body.String())
	
	//Output:
	// <!DOCTYPE html>
	//
	// <html>
	// <head>
	// <meta charset='utf8'></meta>
	// </head>
	//
	// <body>
	// <div class='header'>header
	// <div class='username'>felix</div>
	// </div>
	//
	// <div>This is my home page</div>
	//
	// <div class='footer'>footer</div>
	// </body>
	// </html>

An example show how to set different type of attributes

	type MoreData struct {
	    Name  string
	    Count int
	}
	comp := Div(
	    Input("username").
	        Type("checkbox").
	        Attr("checked", true).
	        Attr("more-data", &MoreData{Name: "felix", Count: 100}).
	        Attr("max-length", 10),
	    Input("username2").
	        Type("checkbox").
	        Attr("checked", false),
	)
	Fprint(os.Stdout, comp, context.TODO())
	//Output:
	// <div>
	// <input name='username' type='checkbox' checked more-data='{"Name":"felix","Count":100}' max-length='10'></input>
	//
	// <input name='username2' type='checkbox'></input>
	// </div>

An example show how to set styles

	comp := Div().
	    StyleIf("background-color:red; border:1px solid red;", true).
	    StyleIf("color:blue", true)
	Fprint(os.Stdout, comp, context.TODO())
	//Output:
	// <div style='background-color:red; border:1px solid red; color:blue;'></div>

An example to use If, Iff is for body to passed in as an func for the body depends on if condition not to be nil, If is for directly passed in HTMLComponent

	type Person struct {
	    Age int
	}
	var p *Person
	
	name := "Leon"
	comp := Div(
	    Iff(p != nil && p.Age > 18, func() HTMLComponent {
	        return Div().Text(name + ": Age > 18")
	    }).ElseIf(p == nil, func() HTMLComponent {
	        return Div().Text("No person named " + name)
	    }).Else(func() HTMLComponent {
	        return Div().Text(name + ":Age <= 18")
	    }),
	)
	Fprint(os.Stdout, comp, context.TODO())
	//Output:
	// <div>
	// <div>No person named Leon</div>
	// </div>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].