• Stars
    star
    578
  • Rank 74,261 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An ERB-style templating language for Go.

Ego GoDoc

Ego is an ERb style templating language for Go. It works by transpiling templates into pure Go and including them at compile time. These templates are light wrappers around the Go language itself.

Install

You can find a release build of ego for Linux on the Releases page.

To install ego from source, you can run this command outside of the GOPATH:

$ go get github.com/benbjohnson/ego/...

Usage

Run ego on a directory. Recursively traverse the directory structure and generate Go files for all matching .ego files.

$ ego mypkg

How to Write Templates

An ego template lets you write text that you want to print out but gives you some handy tags to let you inject actual Go code. This means you don't need to learn a new scripting language to write ego templatesβ€”you already know Go!

Raw Text

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

hello!
goodbye!

Then ego will generate a matching .ego.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 .ego.go file as-is.

For example, given this template:

<%
package myapp

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

The ego tool will generate:

package myapp

import (
	"context"
	"io"
)

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

Note the context and io packages are automatically imported to your template. These are the only packages that do this. You'll need to import any other packages you use.

Print Blocks

Our template is getting more useful. We now have actually runnable Go code. However, our templates typically need output text frequently so there are blocks specifically for this task called print blocks. These print blocks wrap a Go expression with <%= and %> tags.

We can expand our previous example and add a type and fields to our code:

<%
package myapp

type NameRenderer struct {
	Name  string
	Greet bool
}

func (r *NameRenderer) Render(ctx context.Context, w io.Writer) {
%>
	<% if r.Greet { %>
		hello, <%= r.Name %>!
	<% } else { %>
		goodbye, <%= r.Name %>!
	<% } %>
<% } %>

We now have a conditional around our Greet field and we are printing the Name field. Our generated code will look like:

package myapp

import (
	"context"
	"io"
)

type NameRenderer struct {
	Name  string
	Greet bool
}

func Render(ctx context.Context, w io.Writer) {
	if r.Greet {
		io.WriteString(w, "hello, ")
		io.WriteString(w, html.EscapeString(fmt.Sprint(r.Name)))
		io.WriteString(w, "!")
	} else {
		io.WriteString(w, "goodbye, ")
		io.WriteString(w, html.EscapeString(fmt.Sprint(r.Name)))
		io.WriteString(w, "!")
	}
}

Printing unescaped HTML

The <%= %> block will print your text as escaped HTML, however, sometimes you need the raw text such as when you're writing JSON. To do this, simply wrap your Go expression with <%== and %> tags.

Components

Simple code and print tags work well for simple templates but it can be difficult to make reusable functionality. You can use the component syntax to print types that implement this Renderer interface:

type Renderer interface {
	Render(context.Context, io.Writer)
}

Component syntax look likes HTML. You specify the type you want to instantiate as the node name and then use attributes to assign values to fields. The body of your component will be assigned as a closure to a field called Yield on your component type.

For example, let's say you want to make a reusable button that outputs Bootstrap 4.0 code: We can write this component as an ego template or in pure Go code. Here we'll write the component in Go:

package myapp

import (
	"context"
	"io"
)

type Button struct {
	Style string
	Yield func()
}

func (r *Button) Render(ctx context.Context, w io.Writer) {
	fmt.Fprintf(w, `<div class="btn btn-%s">`, r.Style)
	if r.Yield {
		r.Yield()
	}
	fmt.Fprintf(w, `</div>`)
}

Now we can use that component from a template in the same package like this:

<%
package myapp

type MyTemplate struct {}

func (r *MyTemplate) Render(ctx context.Context, w io.Writer) {
%>
	<div class="container">
		<ego:Button Style="danger">Don't click me!</ego:Button>
	</div>
<% } %>

Our template automatically convert our component syntax into an instance and invocation of Button:

var EGO Button
EGO.Style = "danger"
EGO.Yield = func() { io.WriteString(w, "Don't click me!") }
EGO.Render(ctx, w)

Field values can be specified as any Go expression. For example, you could specify a function to return a value for Button.Style:

<ego:Button Style=r.ButtonStyle()>Don't click me!</ego:Button>

Named closures

The Yield is a special instance of a closure, however, you can also specify named closures using the :: syntax.

Given a component type:

type MyView struct {
	Header func()
	Yield  func()
}

We can specify the separate closures like this:

<ego:MyView>
	<ego::Header>
		This content will go in the Header closure.
	</ego::Header>

	This content will go in the Yield closure.
</ego:MyView>

Importing components from other packages

You can import components from other packages by using a namespace that matches the package name The ego namespace is reserved to import types in the current package.

For example, you can import components from a library such as bootstrap-ego:

<%
package myapp

import "github.com/benbjohnson/bootstrap-ego"

type MyTemplate struct {}

func (r *MyTemplate) Render(ctx context.Context, w io.Writer) {
%>
	<bootstrap:Container>
		<bootstrap:Row>
			<div class="col-md-3">
				<bootstrap:Button Style="danger" Size="lg">Don't click me!</bootstrap:Button>
			</div>
		</bootstrap:Row>
	</bootstrap:Container>
<% } %>

Caveats

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

Ego does not attempt to provide any security around the templates. Just like regular Go code, the security model is up to you.

More Repositories

1

litestream

Streaming replication for SQLite.
Go
9,990
star
2

thesecretlivesofdata

Understanding what your bits do when you're not looking.
JavaScript
3,338
star
3

wtf

WTF Dial is an example web application written in Go.
Go
1,533
star
4

postlite

Postgres wire compatible SQLite proxy.
Go
1,202
star
5

immutable

Immutable collections for Go
Go
686
star
6

clock

Clock is a small library for mocking time in Go.
Go
665
star
7

testing

A small collection of functions for Go testing.
Go
525
star
8

megajson

A JSON parser generator for high performance encoding and decoding in Go.
Go
468
star
9

hashfs

Implementation of io/fs.FS that appends SHA256 hashes to filenames to allow for aggressive HTTP caching.
Go
342
star
10

sql-parser

Toy SQL parser example for Gopher Academy
Go
324
star
11

genesis

A simple tool for embedding assets in a Go binary.
Go
300
star
12

phantomjs

Go client for PhantomJS.
Go
294
star
13

jmphash

Implementation of the Jump Consistent Hash algorithm in Go.
Go
154
star
14

scuttlebutt

A daemon for tracking and tweeting trending Github repositories by language.
Go
150
star
15

llvm-c-kaleidoscope

An implementation of the Kaleidoscope language using Flex, Bison & the LLVM-C bindings.
C
129
star
16

playback.js

A library for dynamic timeline playback.
JavaScript
117
star
17

litestream-docker-example

An example of using Litestream within a Docker container.
Go
88
star
18

css

W3C-compliant CSS3 parser and scanner
Go
84
star
19

litestream-s6-example

Example repository for building a multi-process Docker container.
Dockerfile
83
star
20

tmpl

Command line interface to Go's text/template library.
Go
81
star
21

grapevine

Trending topics for stuff you care about
Ruby
80
star
22

slowweb

An HTTP request governor
Ruby
75
star
23

litestream-read-replica-demo

A demo application for running live read replication on fly.io with Litestream
Go
69
star
24

ghfs

FUSE Filesystem for the GitHub API
Go
61
star
25

agency

A fast user agent string parser for Go.
Go
60
star
26

litestream-library-example

Example repository for embedding Litestream in a Go application.
Go
52
star
27

litestream-read-replica-example

An example of using Litestream's live read replication feature.
Go
52
star
28

melomel

External ActionScript Interface.
ActionScript
42
star
29

litestream.io

SCSS
40
star
30

pprofdump

A simple utility for collecting net/http/pprof profiles.
Go
28
star
31

peapod

A personal podcast service.
Go
26
star
32

application-development-using-boltdb

Repository for my "Application Development Using BoltDB" talk
Go
26
star
33

sieve

A command line utility for graphing piped data.
Go
23
star
34

production-sqlite-go

Companion repository for GopherCon presentation on "Production Applications Using SQLite & Go"
Go
23
star
35

goo

Thin wrapper for the Go toolchain.
Go
20
star
36

burger-stack

Presentation for "The Burger Stack"
19
star
37

structuring-applications-for-growth

GopherCon 2016 presentation for "Structuring Applications for Growth"
17
star
38

stack

Go debug/stack utility functions.
Go
17
star
39

myapp

An simple application with an HTTP server & SQLite database.
Go
14
star
40

glee

Incomplete Go port of the KLEE SymEx system.
Go
14
star
41

melomel.rb

An external interface to Flash from Ruby.
Ruby
13
star
42

raft.js

An experimental Raft implementation in Javascript.
13
star
43

vex

Variable-length, lexicographically-sortable hex format for uint64 values.
Go
13
star
44

gha

SQLite load testing application using GitHub Archive data.
Go
13
star
45

writing-a-distributed-systems-library

Companion code for the Gopher Academy blog post.
Go
11
star
46

tiny-ego

A toy application showcasing ego templates & components.
Go
10
star
47

boxer

A little app that boxes my time.
Go
10
star
48

bootstrap-ego

An ego template component library for Bootstrap 4.
Go
10
star
49

seppuku

To be executed upon implementation of generics in Go.
Go
10
star
50

roommate

A conference room scheduling application.
Go
9
star
51

syncutil

A collection of utility functions for Go synchronization.
Go
9
star
52

gist

Gist hosting and embedding.
Go
8
star
53

melomel-examples

Examples of using Melomel.
Ruby
8
star
54

describe.today

A web site to describe today.
JavaScript
7
star
55

minipack

A lightweight C MessagePack parser.
C
7
star
56

skybox

An open source funnel analysis application.
Go
7
star
57

go-raft-runner

A test runner application for the go-raft library.
Go
6
star
58

http-wiretap

It's like Charles Proxy for your Rubies!
Ruby
5
star
59

mincore

Example usage for using mincore() in Go.
Go
5
star
60

miniviz

A simplified interface to GraphViz for laying out clusters, nodes and edges.
Ruby
5
star
61

graphviz-as3

An interface to the graphviz CLI using Adobe AIR.
ActionScript
5
star
62

edb

A simple database for tracking events.
Go
5
star
63

rationl

Online journal for tracking experiments.
Go
4
star
64

sqlite-bench

Miscellaneous Go/SQLite benchmarks
Go
4
star
65

hackerbeeper

A dumb utility for playing generated notes when you type.
Go
4
star
66

opus

Command line utility for printing columnized code.
4
star
67

mockdown.as

A Markdown-inspired Mockup Language
ActionScript
4
star
68

serialkiller

ActionScript JSON & XML serialization library
ActionScript
4
star
69

whodump

A command line interface for checking domain name availability.
Ruby
4
star
70

tsld.js

Core library for "The Secret Lives of Data" project.
JavaScript
3
star
71

bandicoot

A crash reporter library for C applications.
C
3
star
72

skydb.io

The Official Sky Web Site
CSS
2
star
73

cine

A movie search application.
Go
2
star
74

chatter

Demo application using Server Side Events (SSE) and written in Go.
Go
2
star
75

authoritarian

Command line utility for authorizing Twitter users to an application.
2
star
76

matlock

Simple name extraction utility.
Ruby
2
star
77

constdump

Utility for printing a list of package-level constants.
Go
2
star
78

d3.jquery.js

A collection of D3.js charts made available as jQuery plugins.
JavaScript
1
star
79

mockdown.rb

Mockups for hackers
Ruby
1
star
80

landmarkd

The Landmark Tracking Server.
Go
1
star
81

timeshifter

A Ruby library for shifting time.
Ruby
1
star
82

httpng

A local server for saving HTML elements as PNG files.
JavaScript
1
star
83

ldbchk

Runs concurrency tests against LevelDB & Levigo.
Go
1
star
84

whollydeliciousfoods.com

The home page for Wholly Delicious Foods.
JavaScript
1
star
85

homebrew-litestream

Homebrew tap for litestream.
Ruby
1
star
86

bench-c

A small collection of benchmarks for the C programming language.
C
1
star
87

gitcoin

Turing gitcoin repository
Go
1
star
88

unistat

A utility for calculating simple statistics on unicode characters.
Go
1
star
89

termgraf

Terminal chronograf
Go
1
star
90

locald

A simple HTTP server for serving static files out of the current directory.
1
star
91

skylandlabs.github.com

Skyland Labs Blog
JavaScript
1
star
92

fql-editor

An editor for FQL queries.
ActionScript
1
star
93

tip.litestream.io

Mirror of litestream.io repository for latest changes.
HTML
1
star
94

fslice

A small utility for extracting delimited sections of a file.
Go
1
star
95

ego-example

A simple ego templating example.
Go
1
star
96

prog

Go testing
Go
1
star