Coder Social home page Coder Social logo

ego's Introduction

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

ego's People

Contributors

benbjohnson avatar bouk avatar mattn avatar xe avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.