render

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package render provides a template rendering system using the Plush templating engine. It supports layouts, partials, file system-based template loading, and context-aware rendering with helpers and global values. The engine is designed to work with web applications and provides a clean API for rendering HTML templates.

Index

Examples

Constants

This section is empty.

Variables

View Source
var InCtx = Middleware

InCtx puts the render engine in the context so the handlers can use it, it also sets a few other values that are useful for the handlers.

Functions

func Middleware

func Middleware(templates fs.FS, options ...Option) func(http.Handler) http.Handler

Middleware puts the render engine in the context so the handlers can use it, it also sets a few other values that are useful for the handlers.

func TemplateFS

func TemplateFS(embed fs.FS, dir string) templatesFS

NewFallbackFS returns a new FS that wraps the given directory and embedded FS. the embed.FS is expected to embed the same files as the directory FS.

Example
// Create embedded templates
embedTemplates := fstest.MapFS{
	"prod.html": &fstest.MapFile{
		Data: []byte("Production template"),
	},
}

// Create a template filesystem that can fallback between local and embedded
fs := TemplateFS(embedTemplates, "/path/to/templates")

// Use the filesystem with render engine
engine := NewEngine(fs)

result, err := engine.RenderHTML("prod.html", map[string]any{})
if err != nil {
	log.Fatal(err)
}

fmt.Println(result)
Output:

Production template

Types

type Engine

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

func EngineFromCtx

func EngineFromCtx(ctx context.Context) *Engine

FromCtx returns the render engine from the context, it assumes the render render engine has been set by the render middleware.

func NewEngine

func NewEngine(fs fs.FS, options ...Option) *Engine

NewEngine builds the render engine based on the file system and the options passed to it.

Example
// Create a simple filesystem for templates
templates := fstest.MapFS{
	"hello.html": &fstest.MapFile{
		Data: []byte("Hello, <%= name %>!"),
	},
}

// Create a new render engine
engine := NewEngine(templates)

// Render a template to string
result, err := engine.RenderHTML("hello.html", map[string]any{
	"name": "World",
})
if err != nil {
	log.Fatal(err)
}

fmt.Println(result)
Output:

Hello, World!

func (*Engine) HTML

func (e *Engine) HTML(w io.Writer) *Page
Example
templates := fstest.MapFS{
	"greeting.html": &fstest.MapFile{
		Data: []byte("Greetings, <%= user %>!"),
	},
	"app/layouts/application.html": &fstest.MapFile{
		Data: []byte(`<html><body><%= yield %></body></html>`),
	},
}

engine := NewEngine(templates)
var buf bytes.Buffer
page := engine.HTML(&buf)

page.Set("user", "Alice")
err := page.Render("greeting.html")
if err != nil {
	log.Fatal(err)
}

fmt.Println(buf.String())
Output:

<html><body>Greetings, Alice!</body></html>

func (*Engine) RenderHTML

func (e *Engine) RenderHTML(template string, values map[string]any) (string, error)

func (*Engine) Set

func (e *Engine) Set(key string, value any)
Example
templates := fstest.MapFS{
	"welcome.html": &fstest.MapFile{
		Data: []byte("Welcome to <%= site_name %>!"),
	},
}

engine := NewEngine(templates)
// Set global values available to all templates
engine.Set("site_name", "My Website")

result, err := engine.RenderHTML("welcome.html", map[string]any{})
if err != nil {
	log.Fatal(err)
}

fmt.Println(result)
Output:

Welcome to My Website!

func (*Engine) SetHelper

func (e *Engine) SetHelper(key string, value any)
Example
templates := fstest.MapFS{
	"math.html": &fstest.MapFile{
		Data: []byte("Result: <%= multiply(x, y) %>"),
	},
}

engine := NewEngine(templates)
// Set a global helper function
engine.SetHelper("multiply", func(a, b int) int {
	return a * b
})

result, err := engine.RenderHTML("math.html", map[string]any{
	"x": 6,
	"y": 7,
})
if err != nil {
	log.Fatal(err)
}

fmt.Println(result)
Output:

Result: 42

type Option

type Option func(*Engine)

func WithDefaultLayout

func WithDefaultLayout(layout string) Option

WithDefaultLayout sets the default layout for the engine if no layout is specified in the template this layout will be used. By default this is set to "app/layouts/application.html"

Example
templates := fstest.MapFS{
	"page.html": &fstest.MapFile{
		Data: []byte("Content here"),
	},
	"layouts/custom.html": &fstest.MapFile{
		Data: []byte(`<div><%= yield %></div>`),
	},
}

// Create engine with custom default layout
engine := NewEngine(templates, WithDefaultLayout("layouts/custom.html"))

var buf bytes.Buffer
page := engine.HTML(&buf)
page.Render("page.html")

fmt.Println(buf.String())
Output:

<div>Content here</div>

func WithHelpers

func WithHelpers(hps map[string]any) Option

WithHelpers sets the helpers for the engine these helpers will be available in all templates rendered by this engine.

Example
templates := fstest.MapFS{
	"text.html": &fstest.MapFile{
		Data: []byte("Result: <%= shout(message) %>"),
	},
}

// Create engine with helper functions
engine := NewEngine(templates, WithHelpers(map[string]any{
	"shout": func(text string) string {
		return strings.ToUpper(text)
	},
}))

result, err := engine.RenderHTML("text.html", map[string]any{
	"message": "hello world",
})
if err != nil {
	log.Fatal(err)
}

fmt.Println(result)
Output:

Result: HELLO WORLD

type Page

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

func FromCtx

func FromCtx(ctx context.Context) *Page

FromCtx returns the render engine from the context when its called it also adds any value in the valuer into the page, this is useful for the middlewares such as session to add values to the page.

func (*Page) Render

func (p *Page) Render(page string) error

func (*Page) RenderClean

func (p *Page) RenderClean(name string) error
Example
templates := fstest.MapFS{
	"snippet.html": &fstest.MapFile{
		Data: []byte("Name: <%= name %>, Age: <%= age %>"),
	},
}

engine := NewEngine(templates)
var buf bytes.Buffer
page := engine.HTML(&buf)

page.Set("name", "Bob")
page.Set("age", 30)

// Render without any layout
err := page.RenderClean("snippet.html")
if err != nil {
	log.Fatal(err)
}

fmt.Println(buf.String())
Output:

Name: Bob, Age: 30

func (*Page) RenderWithLayout

func (p *Page) RenderWithLayout(page, layout string) error
Example
templates := fstest.MapFS{
	"content.html": &fstest.MapFile{
		Data: []byte("Main content"),
	},
	"special_layout.html": &fstest.MapFile{
		Data: []byte(`<article><%= yield %></article>`),
	},
}

engine := NewEngine(templates)
var buf bytes.Buffer
page := engine.HTML(&buf)

// Render with a specific layout
err := page.RenderWithLayout("content.html", "special_layout.html")
if err != nil {
	log.Fatal(err)
}

fmt.Println(buf.String())
Output:

<article>Main content</article>

func (*Page) Set

func (p *Page) Set(key string, value any)

func (*Page) Value

func (p *Page) Value(key string) any

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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