Giter Club home page Giter Club logo

sanity's Introduction

Sanity

Sanity := Server Side Rendering - Templates

Sanity is a Go library for server-side HTML rendering. Sanity is inspired by the component-based architecture of modern UI toolkits like React. Components are written as normal Go functions that return an html.Node. The advantage of using Go over HTML templating is that Go is more flexible than most HTML templating languages and has better tooling support for type checking and auto-completion. It also allows the use of Go control flow instead of using a mediocure language embedded in the template.

go get github.com/jeffswenson/sanity@latest
package main

import (
	"fmt"

	"github.com/jeffswenson/sanity/pkg/attr"
	"github.com/jeffswenson/sanity/pkg/html"
	"github.com/jeffswenson/sanity/pkg/tag"
)

var fruit = []string { "apple", "orange", "banana" }

// Calling fruitView(fruit) produces the following html:
//
// <ul class="fruit-list">
//     <li>apple</li>
//     <li>orange</li>
//     <li>banana</li>
// </ul>
func fruitView(fruitList []string) html.Node {
	return tag.UL(
		attr.Class("fruit-list"), 
		html.ForEach(fruitList, func(fruit string) html.Node {
			return tag.LI(html.InnerText(fruit)) 
		}),
	)
}

func main() {
    node := fruitView(fruit)
    fmt.Print(node)
}

API

The Sanity API is broken into three packages.

  • pkg/html: contains the core implementation and utilities
  • tag: contains a function for every HTML tag
  • attr: contains a function for every HTML attribute

The tag and attr packages are implemented using public functions from html. So it is possible to create tags and attributes that are not part of the standard by using the functions declared in html.

The function header comments in tag and attr were written by Chat GPT, so take them with a grain of salt. All other documentation and all code was written the old fashioned way.

Testing

Simple components can be tested using the html.Node.String() function. For more complicated structures, use something like the goquery library to parse the generated output.

Performance

The benchmarks contained in the sanity package suggest sanity is more cpu efficient than text/html for templates with many dynamic ({{}}) blocks. Using sanity to generate the HTML is also much cheaper than compressing the HTML with gzip, so sanity views are unlikely to be a significant portion of an application's resource consumption footprint.

sanity's People

Contributors

jeffswenson avatar

Stargazers

 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.