GithubHelp home page GithubHelp logo

blizzy78 / copper Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 243 KB

Template rendering engine for Go

License: MIT License

Shell 0.09% Go 99.91%
go golang template template-rendering copper golang-library

copper's Introduction

Build Status GoDoc Coverage Status

Copper Template Rendering Engine

Copper is a template rendering engine written in Go. It is similar to Buffalo's Plush, but has more capabilities.

Copper is agnostic of any HTTP router or any other framework and can also be used standalone (for example, to render e-mail messages or any other text.)

See Usage Example for a simple standalone example of using Copper.

Template Language

Copper uses a language similar to Go which should be fairly easy to use.

See SYNTAX.md for a full list of language constructs.

No Auto-Escaping Text

Copper never escapes text automatically. Every time text should be rendered, it must be passed through one of the provided (or a custom) helper function that marks the text to be safe for output, optionally escaping it. While this may sound tedious when coming from other rendering engines, it prevents Copper from guessing (and mis-guessing) the correct escaping mechanism to use. Instead, the responsibility is explicitly shifted to the author of the template.

Usage Example

This is a simple, but full example of how to use Copper. It shows how to render a simple template that outputs some dynamic variables.

See Copper Example for a more complete example that integrates into net/http, Gorilla, Chi, or httprouter.

package main

import (
	"bytes"
	"context"
	"io"
	"strings"

	"github.com/blizzy78/copper/helpers"
	"github.com/blizzy78/copper/template"
)

const (
	// the template to render
	tmpl = `<html>
<body>
<p>Hello, <% html(who) %>!</p>
<% safe(someHTML) %>
</body>
</html>`
)

func main() {
	// load a template by name -
	// in this example, we ignore the name and always return the same template
	loader := template.LoaderFunc(func(name string) (io.ReadCloser, error) {
		return io.NopCloser(strings.NewReader(tmpl)), nil
	})

	// construct a new renderer
	r := template.NewRenderer(loader,
		// html will be a global template function that HTML-escapes strings and
		// marks them as safe for output
		template.WithScopeData("html", helpers.HTML),

		// safe will be a global template function that marks strings as safe
		// for output
		template.WithScopeData("safe", helpers.Safe))

	// a context that can be used by template helper functions -
	// the renderer does not use it
	ctx := context.Background()

	// let's render into this buffer - any io.Writer is fine
	buf := bytes.Buffer{}

	// the name of the template to render -
	// in this example, the name is ignored
	name := "myTemplate"

	// data provided to the template being rendered
	data := map[string]interface{}{
		"who":      "World",
		"someHTML": "<p>This is HTML</p>",
	}

	// parse and render the template
	err := r.Render(ctx, &buf, name, data)
	if err != nil {
		panic(err)
	}

	// output buffer contents
	println(buf.String())
}

Usage on Command Line

You can use the coppercli tool to render text on the command line.

License

Copper is licensed under the MIT license.

copper's People

Contributors

blizzy78 avatar

Watchers

 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.