Documentation - Chat - Playground
Eta is a lightweight embedded JS templating engine: a template mixes text with tags holding
JavaScript, and Eta compiles it into a JavaScript function that renders it. This repository is Eta
4.6.0 in Go: the module github.com/bgub/eta-go reproduces the eta and eta/core entries of the
npm package, and runs the JavaScript inside templates with its own interpreter, which follows V8
(Node.js 24): the generated source, the output and the error messages are the original's.
- No dependencies outside the Go standard library
- Plugins, custom delimiters, custom tags, caching
- Partials, layouts, blocks and capture, async templates
- Whitespace control (
autoTrim,-and_tag markers,rmWhitespace) - Error reporting with the line and column of parse errors and, with
debug, the template lines around a runtime error
go get github.com/bgub/eta-goIn the root of your project, create templates/simple.eta
Hi <%= it.name %>!
Then, in your Go program:
package main
import (
"fmt"
"log"
eta "github.com/bgub/eta-go"
"github.com/bgub/eta-go/js"
)
func main() {
e, err := eta.New(eta.O("views", "templates"))
if err != nil {
log.Fatal(err)
}
res, err := e.Render("./simple", eta.O("name", "Ben"), js.Undefined)
if err != nil {
log.Fatal(err)
}
fmt.Println(res) // Hi Ben!
}Data, configuration and options are JavaScript values, modelled by package
github.com/bgub/eta-go/js: js.Undefined is undefined, Go nil is null, numbers are
float64, eta.O(k1, v1, k2, v2, ...) builds an object with keys in order, eta.A(...) an array,
and a *js.Function is a function, either written in Go (&js.Function{Name, Length, Call}) or
returned by a template:
helpers, _ := e.RenderString(`<% return {shout(s) { return String(s).toUpperCase() + "!" }} %>`, eta.O())
out, _ := e.RenderString(`<%= it.h.shout(it.name) %>`, eta.O("h", helpers, "name", "Ben"))
// out == "BEN!"A template that throws makes the call return the thrown value as the error: a *js.Error (with
the original's Name and Message, such as EtaParser Error or TypeError) or a *js.Thrown.
| JavaScript | Go |
|---|---|
new Eta(config) from eta |
eta.New(config) |
new Eta(config) from eta/core |
eta.NewCore(config) |
eta.render(name or fn, data, meta) |
e.Render(template, data, meta) |
eta.renderAsync(...) |
e.RenderAsync(...) (waits for the promise) |
eta.renderString(str, data) |
e.RenderString(str, data) |
eta.renderStringAsync(str, data) |
e.RenderStringAsync(str, data) |
eta.compile(str, options) |
e.Compile(str, options) |
eta.compileToString(str, options) |
e.CompileToString(str, options) |
eta.parse(str) / eta.compileBody(ast) |
e.Parse(str) / e.CompileBody(ast) |
eta.loadTemplate(name, template, options) |
e.LoadTemplate(name, template, options) |
eta.configure(config) / eta.withConfig(config) |
e.Configure(config) / e.WithConfig(config) |
eta.resolvePath(name, options) / eta.readFile(path) |
e.ResolvePath(name, options) / e.ReadFile(path) |
eta.templatesSync, eta.templatesAsync, eta.filepathCache |
e.TemplatesSync(), e.TemplatesAsync(), e.FilepathCache() |
go vet ./...
go test ./...The interpreter lives in internal/jsrt (with internal/jsregexp for regular expressions and
internal/jsdate for dates); the root package is the port of Eta's own source
8EAA
(parse,
compileToString, compile, render, file handling and the template stores).
The interpreter has no Intl and no locale data, so localeCompare, String.prototype.normalize
and the locale arguments of toLocaleString are absent (toLocaleString formats as Node does in
the en-US default); iterator helpers (Iterator.prototype.map and the rest) are absent too. Using
one fails the way calling a missing method does in JavaScript. Node's host objects (process,
require, Buffer) are not provided either.
Where did Eta's name come from?
"Eta" means tiny in Esperanto. Plus, it can be used as an acronym for all sorts of cool phrases: "ECMAScript Template Awesomeness", "Embedded Templating Alternative", etc....
Additionally, Eta is a letter of the Greek alphabet (it stands for all sorts of cool things in various mathematical fields, including efficiency) and is three letters long (perfect for a file extension).
Visual Studio Code
@shadowtime2000 created eta-vscode.
ESLint
eslint-plugin-eta was created to provide an ESLint processor so you can lint your Eta templates.
Webpack
Currently there is no official Webpack integration but @clshortfuse shared the loader he uses:
{
loader: 'html-loader',
options: {
preprocessor(content, loaderContext) {
return eta.render(content, {}, { filename: loaderContext.resourcePath });
},
},
}Koa
To render Eta templates in Koa web framework: @cedx/koa-eta
Vite
To use Eta templates in your Vite project: @rinoshiyo/vite-plugin-eta
- Docusaurus v2: open-source documentation framework that uses Eta to generate a SSR build
- swagger-typescript-api: Open source typescript api codegenerator from Swagger. Uses Eta as codegenerator by templates
- html-bundler-webpack-plugin: Webpack plugin make easily to bundle HTML pages from templates, source styles and scripts
- SmartDeno: SmartDeno is an easy to setup web template using Deno & Oak
- stc: OpenAPI (Swagger) and Apifox documentation converted to api. Use eta templates to generate code.
- Add yours!
Made with ❤️ by bgub (Ben Gubler) and many wonderful contributors. Contributions of any kind are welcome!
- Async support, file handling, and error formatting were based on code from EJS, which is licensed under the Apache-2.0 license. Code was modified and refactored to some extent.
- Syntax and some parts of compilation are heavily based off EJS, Nunjucks, and doT.