Frongo is a Golang package to create HTML/CSS components using only the Go language.

Frongo

Frongo is a Go tool to make HTML/CSS document out of Golang code. It was designed with readability and usability in mind, so HTML objects are created by chaining method calls.

Quick example :

div := *frongo.Div()

Initializes a div to be inserted into the document later on.

div.Div().Class("class").Attr("id", "testId").Write("some text")

Creates a div tag inside the div object, and adds a class, an attribute and some text into it.

But you can also get the same result by using :

div.Div("class", "id", "testId").Write("some text")

However the first way is recommended since it optimizes readability.

Either way, You will obtain this :

<div>
    <div class="class" id="testId">
        some text
    </div>
</div>

Installation

Like most Go packages, the installation is extremely simple :

$ go get -u github.com/eleby/frongo

And now you can use Frongo in your project.

Usage

Frongo reproduces the standard structure for a web document :

Document
----Containers
--------Tags
----Sheets
--------Styles

So in Frongo, the first thing to do is to create a document.

doc := frongo.InitDocument()

Then we want to create the objects we will use in our document.

body := *frongo.InitContainer("body")
sheet := frongo.Sheet{}

Once we have done these initializations, we can get to coding our html and css elements.

div := *frongo.NewTag("div")

The "NewTag" function can be replaced by presets to improve readability as well as usability :

title := *frongo.Title("title text")
div := *frongo.Div()
span := *frongo.Span()
paragraph := *frongo.P()
img := *frongo.Img()
lineJump := *frongo.Br()

All these presets can also be used when adding a tag's child, like div.Div() which adds a div to the first div.

All presets except Title and Br use the "params" parameter which indicates a potential class and list of attributes, attribute values and delimiter between each attribute name/value. All these are optional, and are shown (except the delimiter which is "," by default) in the next example. The Title preset uses a mandatory string as the text to use as the title.

So once we have our div, we can easily add classes and attributes; there are many ways to do this.

At initialization :

div := *frongo.Div("class", "attr1,attr2", "val1,val2")

Later, all at once :

div.Class("class").Attrs("attr1,attr2", "val1,val2")

Or later, one by one :

div.Class("class").Attr("attr1", "val1").Attr("attr2", "val2")

You can also make a mix :

div := *frongo.Div("class").Attr("attr1", "val1").Attr("attr2", "val2")

Having all these possibilities allow you to manage your code's length and readability so you can use it the way which fits you.

If you want to add text to a div, you can use the Write method to write IN the div (before its children) or WriteAfter to write after the div.

div.Write("some text") //<div>some text</div>
div.WriteAfter("some text") //<div></div>some text

Now if you want to close the tag (so it will be a self-closing one) you can use the Close method.

linkTag.Close()

So now we have seen the basics for HTML tags. There are also many methods to find them by tag name or by id, or index :

div.FindById("testId") //Gets the first found element with id
div.Find("p") //Gets an array of all the children p tags
div.First() //Gets the first child of the div
div.Get(1) //Gets the second child of the div
div.Last() //Gets the last child of the div

The Find method also can be called on the document object once the tags are added.

There are also check methods :

document.Contains("div") //Returns true if there is a div in the document.
div.HasChild("p") //Returns true if there are p tags in the div's children.
div.HasChild("p", true) //Returns true if there are p tags in the div's children, recursive.
div.HasAttr("attr") //Returns true if the div has the attr attribute.
div.HasAttrValue("attr", "value") //Returns true if the div has the attr attribute set to "value".

And methods to remove attributes :

div.RmClass("class") //Removes the div's class attribute.
div.RmAttr("attr") //Removes the div's "attr" attribute.

Once your HTML tags are ready, you can add them to the body container this way :

body.Put(div) //Adds the div to the body 
body.Make(div, div2, div3) //Adds a list of divs to the body

And then we add our HTML to the document :

doc.Put(body) //Adds the body container to the document. 
doc.Make(header, body, footer) //Adds several containers to the document

Now the HTML is ready. Let's take a look at css styling with our sheet object :

style := *frongo.NewStyle("selector", "modifiers", "values") //Creates a style object
sheet.Put(style) //Adds a style object to the sheet
sheet.Make(style, otherStyle) //Adds many style objects to the sheet
sheet.Style("selector", "modifiers", "values") //Adds a new style, this method can be chained

Then we add the sheet to the document :

doc.Style(sheet, otherEventualSheet) //Adds a list of sheets to the document

Now, the document is ready. You can either get a HTML template out of it or save it as a file :

doc.Render() //Returns the complete HTML template
body.Render() //Returns the body's HTML template
sheet.Render() //Returns the sheet's HTML template

frongo.Create("filename", doc.Render()) //Creates a file for the document, indirect way

doc.Create("filename") //Creates a file for the document
body.Create("filename") //Creates a file for the body
sheet.Create("filename") //Creates a file for the sheet

Now everything is set and you can use either the resulting file or the resulting template. And everything was made using only Go !

Owner
Rewan_
Development is the best kind of art.
Rewan_
Similar Resources

yview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.

wview wview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application. Contents Instal

Dec 5, 2021

Golang library for converting Markdown to HTML. Good documentation is included.

md2html is a golang library for converting Markdown to HTML. Install go get github.com/wallblog/md2html Example package main import( "github.com/wa

Jan 11, 2022

A little like that j-thing, only in Go.

goquery - a little like that j-thing, only in Go goquery brings a syntax and a set of features similar to jQuery to the Go language. It is based on Go

Dec 30, 2022

⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules.

⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules.

html-to-markdown Convert HTML into Markdown with Go. It is using an HTML Parser to avoid the use of regexp as much as possible. That should prevent so

Jan 6, 2023

Produces a set of tags from given source. Source can be either an HTML page, Markdown document or a plain text. Supports English, Russian, Chinese, Hindi, Spanish, Arabic, Japanese, German, Hebrew, French and Korean languages.

Produces a set of tags from given source. Source can be either an HTML page, Markdown document or a plain text. Supports English, Russian, Chinese, Hindi, Spanish, Arabic, Japanese, German, Hebrew, French and Korean languages.

Tagify Gets STDIN, file or HTTP address as an input and returns a list of most popular words ordered by popularity as an output. More info about what

Dec 19, 2022

Templating system for HTML and other text documents - go implementation

FAQ What is Kasia.go? Kasia.go is a Go implementation of the Kasia templating system. Kasia is primarily designed for HTML, but you can use it for any

Mar 15, 2022

export stripTags from html/template as strip.StripTags

HTML StripTags for Go This is a Go package containing an extracted version of the unexported stripTags function in html/template/html.go. ⚠️ This pack

Dec 4, 2022

network .md into .html with plaintext files

network .md into .html with plaintext files

plain network markdown files into html with plaintext files plain is a static-site generator operating on plaintext files containing a small set of co

Dec 10, 2022

Simple Markdown to Html converter in Go.

Markdown To Html Converter Simple Example package main import ( "github.com/gopherzz/MTDGo/pkg/lexer" "github.com/gopherzz/MTDGo/pkg/parser" "fm

Jan 29, 2022
bluemonday: a fast golang HTML sanitizer (inspired by the OWASP Java HTML Sanitizer) to scrub user generated content of XSS

bluemonday bluemonday is a HTML sanitizer implemented in Go. It is fast and highly configurable. bluemonday takes untrusted user generated content as

Jan 4, 2023
Take screenshots of websites and create PDF from HTML pages using chromium and docker

gochro is a small docker image with chromium installed and a golang based webserver to interact wit it. It can be used to take screenshots of w

Nov 23, 2022
A simple CSS parser and inliner in Go

douceur A simple CSS parser and inliner in Golang. Parser is vaguely inspired by CSS Syntax Module Level 3 and corresponding JS parser. Inliner only p

Dec 12, 2022
A simple programming language with emojis only

MOGEE mogee (pronounced ēˈmōjē) is an extremely simple interpreted programming language that consists solely of emojis. Similar to Brainfuck, mogee is

Nov 15, 2021
htmlquery is golang XPath package for HTML query.

htmlquery Overview htmlquery is an XPath query package for HTML, lets you extract data or evaluate from HTML documents by an XPath expression. htmlque

Jan 4, 2023
HomeDashboard renderer core components

HomeDashboard Renderer Core Components Contains core components and basic render

Jan 5, 2022
A declarative struct-tag-based HTML unmarshaling or scraping package for Go built on top of the goquery library

goq Example import ( "log" "net/http" "astuart.co/goq" ) // Structured representation for github file name table type example struct { Title str

Dec 12, 2022
Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and struct tags for golang crawler
Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and struct tags for golang crawler

Pagser Pagser inspired by page parser。 Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and str

Dec 13, 2022
Golang HTML to plaintext conversion library

html2text Converts HTML into text of the markdown-flavored variety Introduction Ensure your emails are readable by all! Turns HTML into raw text, usef

Dec 28, 2022
golang program that simpily converts html into markdown

Simpily converts html to markdown Just a simple project I wrote in golang to convert html to markdown, surprisingly works decent for a lot of websites

Oct 23, 2021