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 ¶
- type Context
- type Template
- func (t *Template) Attribute(key string) types.Attribute
- func (t *Template) Execute(wr io.Writer, data *Context) error
- func (t *Template) HTML() *htmltemplate.Template
- func (t *Template) Parse(element types.Element) (*Template, error)
- func (t *Template) Placeholder(key string) types.Element
- func (t *Template) SetFallbackAttribute(key string, attribute types.Attribute) error
- func (t *Template) SetFallbackContent(key string, element types.Element) error
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Parse accepts a godom element and parses it as the template body. The element is immediately rendered into the template.
func (*Template) Placeholder ¶
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 ¶
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 ¶
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.