GithubHelp home page GithubHelp logo

ego's Introduction

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.

ego's People

Contributors

benbjohnson avatar bouk avatar daaku avatar mattn avatar xe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ego's Issues

Definition Receivers

Change the DefinitionBlock to specify the full signature. This will allow for global templates or templates with receivers.

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

Versioning

Is there a recommended way to make sure all people working on a project using ego use the same version of ego? Different versions of ego can lead to unnecessary overwrites of the generated ego.go file without changes to the source templates.

v0.4.2 removes necessary whitespace

My HTML pages are breaking because the v0.4.2 release strips the whitespace between these ego tags:

<header class="row">
  <div class="col-5">
    <h3><%= t(req, "Batch") %> <%= st.Bid %></h3>
  </div>
</header>

The generated <h3> should contain Batch abc123 but contains Batchabc123. v0.4.1 works fine.

Auto Import

Automatically import the fmt and io packages.

suppress leading and trailing whitespace

We are using ego for one of our newer services. Very nice indeed 👍

One thing is annoying though:

From the Rails ERB Documentation:

When on a line that only contains whitespaces except for the tag, <% %> suppress leading and trailing whitespace, including the trailing newline. <% %> and <%- -%> are the same. Note however that <%= %> and <%= -%> are different: only the latter removes trailing whitespaces.

Maybe ego could handle it in the same way? Especially linebreaks are a problem when writing i.e. markdown.

Handle "%>" inside Go strings

Currently ego does not allow %> inside Go strings that are in blocks. It thinks its just a block closing. This should be fixed.

README: go get updates go mod in go1.16

From the README executing go get github.com/benbjohnson/ego/... will just update go.mod and go.sum if executed from inside the project directory with go 1.16. I believe this is because GO111MODULE is now set to on by default.

go install github.com/benbjohnson/ego@latest will work from any directory, but only in go 1.16. The wtf dial project relies on go1.16 which is why I installed ego and used this alternative command.

ego confused by 'bulletproof button'

It's a common pattern in email templates to use 'bulletproof buttons'
https://www.litmus.com/blog/a-guide-to-bulletproof-buttons-in-email-design/

but this confuses ego, e.g. this snippet of HTML in an ego template

<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                        <td align="center">
                            <div>
                            <!--[if mso]><v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="<%=gifturl%>" style="height:45px;v-text-anchor:middle;width:200px;" arcsize="7%" stroke="f" fill="t">
                            <v:fill type="tile" color="#0072ce" />
                            <w:anchorlock/>
                            <center style="color:#ffffff;font-family:sans-serif;font-size:15px;">Get my gift!</center>
                            </v:roundrect><![endif]-->
                            <a href="<%=gifturl%>" class="button button--blue">Get my gift</a>
                            </div>
                        </td>
                        </tr>
                    </table>

causes ego to emit

//line email_gift_fulfilled_html.ego:33
	_, _ = io.WriteString(w, html.EscapeString(fmt.Sprint(lib.FormatMoney(enrollment.Plan.GiftValue, 0, 2, false, true))))
//line email_gift_fulfilled_html.ego:33
	_, _ = io.WriteString(w, " gift. It's now ready and \n                    you can pick it up here:\n                    </p>\n                    <table class=\"body-action\" align=\"center\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n                        <tr>\n                        <td align=\"center\">\n                            <div>\n                            <!--[if mso]>")
//line email_gift_fulfilled_html.ego:40
	{
		var EGO v.roundrect
		EGO.Attrs = map[string]string{
			"xmlns:v": fmt.Sprint("urn:schemas-microsoft-com:vml"),
			"xmlns:w": fmt.Sprint("urn:schemas-microsoft-com:office:word"),
			"href":    fmt.Sprint("<%=gifturl%>"),
			"style":   fmt.Sprint("height:45px;v-text-anchor:middle;width:200px;"),
			"arcsize": fmt.Sprint("7%"),
			"stroke":  fmt.Sprint("f"),
			"fill":    fmt.Sprint("t"),
		}
		EGO.Yield = func() {
//line email_gift_fulfilled_html.ego:41
			_, _ = i

which causes a subsequent compilation error

undefined: v in v.roundrect
undefined: v in v.fill
...etc...

undefined: User

Running your example, I get undefined: User
What could be a problem? Thanks.

New release?

Would it be possible to tag the current master version as 0.3.0?

I'm currently working on creating a FreeBSD port for faktory. This uses the current master ego for its webui. To keep things clean, I'm also writing a FreeBSD port for ego. As the templates used by faktory aren't compatible with ego 0.2.0, it'd be great to be able to package a new release of ego.

Render time support?

Has anyone added or know how to add rendering time output for templates? I have some slow pages and it would be a huge benefit to see something like:

<!-- foo.ego in 43.1234ms -->

in the page source. It would allow me to narrow down view performance issues to a specific template very quickly.

HTML Santization

Provide a flag on the command line for automatically HTML-escaping print blocks.

Default Package Name

If a package name is not specified in the first header block then automatically inject the directory's name.

take a look at my enhancements

Hi @benbjohnson,

could you please take a look at my fork (https://github.com/SlinSo/egon), that is unfortunately based on another fork (https://github.com/commondream/egon). I would like to know, if it is worth to rebase everything to your original version of ego.

I've made this fork after playing around with some bechmarks I made (https://github.com/SlinSo/goTemplateBenchmark)

I've added a few features and optimisations:

  • Added flags to the egon command (based on kingpin package)
  • Added type safe generation like in ftmpl
  • Added Debug mode, otherwise dont print comments inside generated functions
  • Added string optimisation, removed Sprintf for strings
  • Added examples for framework integration
  • Added minify, currently only tested with HTML code
  • Added optional view generation (only needed for egon fork)
  • Reverted fmt.Fprintf -> io.WriteString to avoid unnecessary allocations (only needed for egon fork)

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.