All Projects → benbjohnson → Ego

benbjohnson / Ego

Licence: mit
An ERB-style templating language for Go.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Ego

Automad
A flat-file content management system and template engine
Stars: ✭ 163 (-65.83%)
Mutual labels:  template-language
gender-render
Template-system and proof-of-concept for rendering gender-neutral text-, email- and RPG-text-templates with the correct pronouns of all people involved.
Stars: ✭ 21 (-95.6%)
Mutual labels:  template-language
closet
The Web Framework for Flashcards
Stars: ✭ 45 (-90.57%)
Mutual labels:  template-language
Slang
Slim-inspired templating language for Crystal
Stars: ✭ 200 (-58.07%)
Mutual labels:  template-language
Superviews.js
Template engine targeting incremental-dom
Stars: ✭ 242 (-49.27%)
Mutual labels:  template-language
Splain
small parser to create more interesting language/sentences
Stars: ✭ 15 (-96.86%)
Mutual labels:  template-language
Ehtml
HTML Framework that allows you not to write JavaScript code.
Stars: ✭ 161 (-66.25%)
Mutual labels:  template-language
Rascal
The implementation of the Rascal meta-programming language (including interpreter, type checker, parser generator, compiler and JVM based run-time system)
Stars: ✭ 284 (-40.46%)
Mutual labels:  template-language
escapevelocity
A subset reimplementation of Apache Velocity with a much simpler API.
Stars: ✭ 28 (-94.13%)
Mutual labels:  template-language
khufu
A template language for incremental-dom or DSL for javascript views
Stars: ✭ 19 (-96.02%)
Mutual labels:  template-language
Htl Spec
HTML Template Language Specification
Stars: ✭ 213 (-55.35%)
Mutual labels:  template-language
Liquid Rust
Liquid templating for Rust
Stars: ✭ 229 (-51.99%)
Mutual labels:  template-language
karkas
A tiny template engine based on TypeScript
Stars: ✭ 14 (-97.06%)
Mutual labels:  template-language
Milk
Milk is Mustache in CoffeeScript -- great with your browser or NodeJS!
Stars: ✭ 192 (-59.75%)
Mutual labels:  template-language
hyperviews
Template language that produces `h` output
Stars: ✭ 26 (-94.55%)
Mutual labels:  template-language
Pongo2
Django-syntax like template-engine for Go
Stars: ✭ 2,111 (+342.56%)
Mutual labels:  template-language
strgen
A Python module for a template language that generates randomized data
Stars: ✭ 34 (-92.87%)
Mutual labels:  template-language
Leaf
🍃 An expressive, performant, and extensible templating language built for Swift.
Stars: ✭ 310 (-35.01%)
Mutual labels:  template-language
bart
A compile time templating language for Rust inspired by Mustache
Stars: ✭ 29 (-93.92%)
Mutual labels:  template-language
sourcery-templates
Building Vapor projects using meta programming with Sourcery ✨
Stars: ✭ 24 (-94.97%)
Mutual labels:  template-language

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.

Trim Space

The <% %> blocks can be optionally adorned with a - on one or both sides to trigger trimming of whitespace on that side. For example <%-= r.Name %> will trim whitespace before the print block, but not after it.

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.

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].