ego

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2015 License: MIT Imports: 10 Imported by: 0

README

Ego Build Status Coverage Status GoDoc Project status

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.

Usage

To install ego:

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

Then run ego on a directory. Recursively traverse the directory structure and compile all .ego files.

$ ego mypkg

All ego files found in a package are compiled and written to a single ego.go file. The name of the directory is used as the package name.

Language Definition

An ego template is made up of several types of blocks:

  • Code Block - These blocks execute raw Go code: <% var foo = "bar" %>

  • Print Block - These blocks print a Go expression. They use html.EscapeString to escape it before outputting: <%= myVar %>

  • Raw Print Block - These blocks print a Go expression raw into the HTML: <%== "<script>" %>

  • Header Block - These blocks allow you to import packages: <%% import "encoding/json" %%>

  • Declaration Block - This block defines the function signature for your template.

A single declaration block should exist at the top of your template and accept an w io.Writer and return an error. Other arguments can be added as needed. A function receiver can also be used.

<%! func MyTmpl(w io.Writer) error %>

Example

Below is an example ego template for a web page:

<%! func MyTmpl(w io.Writer, u *User) error %>

<%% import "strings" %%>

<html>
  <body>
    <h1>Hello <%= strings.TrimSpace(u.FirstName) %>!</h1>

    <p>Here's a list of your favorite colors:</p>
    <ul>
      <% for _, colorName := range u.FavoriteColors { %>
        <li><%= colorName %></li>
      <% } %>
    </ul>
  </body>
</html>

Once this template is compiled you can call it using the definition you specified:

myUser := &User{
  FirstName: "Bob",
  FavoriteColors: []string{"blue", "green", "mauve"},
}
var buf bytes.Buffer
err := mypkg.MyTmpl(&buf, myUser)

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDeclarationRequired is returned when there is no declaration block
	// in a template.
	ErrDeclarationRequired = errors.New("declaration required")
)

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 DeclarationBlock

type DeclarationBlock struct {
	Pos     Pos
	Content string
}

DeclarationBlock represents a block that declaration the function signature.

type HeaderBlock

type HeaderBlock struct {
	Pos     Pos
	Content string
}

HeaderBlock represents a Go code block that is printed at the top of the template.

type Package

type Package struct {
	Name      string
	Templates []*Template
}

Package represents a collection of ego templates in a single package.

func (*Package) Write

func (p *Package) Write(w io.Writer) error

Write writes out the package header and templates to a writer.

type Pos

type Pos struct {
	Path   string
	LineNo int
}

Pos represents a position in a given file.

type PrintBlock

type PrintBlock struct {
	Pos     Pos
	Content string
}

PrintBlock represents a block that will HTML escape the contents before outputting

type RawPrintBlock

type RawPrintBlock struct {
	Pos     Pos
	Content string
}

RawPrintBlock represents a block of the template that is printed out to the writer.

type Scanner

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

Scanner is a tokenizer for Ego 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 Template

type Template struct {
	Path   string
	Blocks []Block
}

Template represents an entire Ego template. A template consists of a declaration block followed by zero or more blocks. Blocks can be either a TextBlock, a PrintBlock, a RawPrintBlock, or a CodeBlock.

func Parse

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

Parse parses an Ego 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 an Ego template from a file.

func (*Template) Write

func (t *Template) Write(w io.Writer) error

Write writes the template to a writer.

type TextBlock

type TextBlock struct {
	Pos     Pos
	Content string
}

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

Directories

Path Synopsis
cmd
ego

Jump to

Keyboard shortcuts

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