template

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package template provides a high-level wrapper around the standard html/template package, enabling seamless integration with the godom library.

The template package bridges the gap between godom and standard templates, focusing on improving performance and user experience. Unlike the upstream html/template package, it currently does not support parsing multiple related templates.

Notably, when using this package, users must provide an access their own data within the `UserData` member of the Context, limiting customization.

Features: - Seamless integration with godom elements. - Provides utility functions to manage placeholders and attributes. - Offers a mechanism to define fallback content and attributes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Placeholders map[string]types.Element
	Attributes   map[string]types.Attribute
	UserData     any
}

Context represents the data structure used during the template execution. It defines placeholders, attributes, and user data to customize the template rendering.

type Template

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

Template is a high-level structure that wraps the standard html/template.Template. It integrates godom elements, allowing them to be treated as HTML templates.

func Must

func Must(t *Template, err error) *Template

Must is a utility function that accepts a template and an error. If the error is non-nil, it panics; otherwise, it returns the template. This is useful for ensuring that template creation operations are successful without requiring explicit error handling.

func New

func New(name string) *Template

New initializes and returns a new Template with the specified name. It sets up the necessary helper functions and data structures for rendering godom elements.

func (*Template) Attribute

func (t *Template) Attribute(key string) types.Attribute

Attribute defines an attribute placeholder in the template. The returned godom attribute represents the placeholder in template syntax. Initially, it's set to an empty attribute.

func (*Template) Execute

func (t *Template) Execute(wr io.Writer, data *Context) error

Execute wraps the underlying template's Execute method. It renders the template with the provided context data and writes the output to the specified writer.

func (*Template) HTML

func (t *Template) HTML() *htmltemplate.Template

HTML provides direct access to the underlying html/template.Template of the wrapper.

func (*Template) Parse

func (t *Template) Parse(element types.Element) (*Template, error)

Parse accepts a godom element and parses it as the template body. The element is immediately rendered into the template.

func (*Template) Placeholder

func (t *Template) Placeholder(key string) types.Element

Placeholder defines a placeholder in the template that can later be replaced with actual content. The returned godom element represents the placeholder in template syntax. Initially, it's set to an empty element.

func (*Template) SetFallbackAttribute

func (t *Template) SetFallbackAttribute(key string, attribute types.Attribute) error

SetFallbackAttribute specifies the default attribute for a placeholder. If no attribute is provided during the template execution, this fallback attribute will be used. The provided godom attribute will be rendered each time the template is executed.

func (*Template) SetFallbackContent

func (t *Template) SetFallbackContent(key string, element types.Element) error

SetFallbackContent specifies the default content for a placeholder. If no content is provided during the template execution, this fallback content will be used. The provided godom element will be rendered each time the template is executed.

Jump to

Keyboard shortcuts

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