got

package module
v0.0.0-...-e00a026 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2020 License: MIT Imports: 11 Imported by: 0

README

Got - Type safe Go(lang) templating

Got is a super-easy templating language for Go. It works by transpiling templates into pure Go and including them at compile time. These templates are lightweight wrappers around the Go language itself.

Usage

To install Got:

$ go get github.com/albertocaleffi/got/...

Then run got on a directory, it recursively traverse the directory structure and generate Go files for all matching .got files.

$ got mypkg

How to Write Templates

A got template lets you write text that you want to print out lets you inject actual Go code. This means you don't need to learn a new scripting language to write got templates—you already know Go!

Raw Text

Any text the got tool encounters that is not wrapped in <% and %> tags is considered raw text. If you have a template like this:

hello!
goodbye!

Then got will generate a matching .got.go file:

io.WriteString(w, "hello!\ngoodbye!")

Unfortunately that file won't run because we're missing a package line at the top. We can fix that with code blocks.

Code Blocks

A code block is a section of your template wrapped in <% and %> tags. It is raw Go code that will be inserted into our generate .got.go file as-is.

For example, given this template:

<%
package myapp

import "io"

func Render(w io.Writer) {
%>
hello!
goodbye!
<%
}
%>

The got tool will generate:

package myapp

import "io"

func Render(w io.Writer) {
	io.WriteString(w, "hello!\ngoodbye!")
}

Note the io package must be imported to your template. You'll need to import any other packages you use.

Caveats

Unlike other runtime-based templating languages, Got does not support ad hoc templates. All Got templates must be generated into Go code before compile time.

Got does not attempt to provide any security around the templates. Just like regular Go code, the security model is up to you. The text rendered in Got templates must be HTML-escaped to prevent XSS (Cross-Site Scripting) and other web vulnerabilities.

Credits

Got is based on the Ego templating package by Ben Johnson.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block interface {
	// contains filtered or unexported methods
}

Block represents an element of the template.

type CodeBlock

type CodeBlock struct {
	Pos     Pos
	Content string
}

CodeBlock represents a Go code block that is printed as-is to the template.

type Pos

type Pos struct {
	Path   string
	LineNo int
}

Pos represents a position in a given file.

func Position

func Position(blk Block) Pos

Position returns the position of the block.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner is a tokenizer for got templates.

func NewScanner

func NewScanner(r io.Reader, path string) *Scanner

NewScanner initializes a new scanner with a given reader.

func (*Scanner) Scan

func (s *Scanner) Scan() (Block, error)

Scan returns the next block from the reader.

type SyntaxError

type SyntaxError struct {
	Message string
	Pos     Pos
}

func NewSyntaxError

func NewSyntaxError(pos Pos, format string, args ...interface{}) *SyntaxError

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type Template

type Template struct {
	Path   string
	Blocks []Block
}

Template represents an entire got template. A template consists of zero or more blocks. Blocks can be either a TextBlock or a CodeBlock.

func Parse

func Parse(r io.Reader, path string) (*Template, error)

Parse parses a got template from a reader. The path specifies the path name used in the compiled template's pragmas.

func ParseFile

func ParseFile(path string) (*Template, error)

ParseFile parses a got template from a file.

func (*Template) WriteTo

func (t *Template) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes the template to a writer.

type TextBlock

type TextBlock struct {
	Pos     Pos
	Content string
}

PassBlock represents a UTF-8 encoded block of text that is written to the writer as-is.

Directories

Path Synopsis
cmd
got

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL