template

package
v0.0.0-...-a5bc9d5 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 34 Imported by: 1

Documentation

Index

Constants

View Source
const MaxTemplateSize = 1024 * 1024 * 1

MaxTemplateSize is 1MiB.

View Source
const TranslationJSONName = "translation.json"

Variables

View Source
var ErrLimitReached = errors.New("template: rendered template is too large")
View Source
var ErrMaxIteractionExceed = fmt.Errorf("max iteration exceeded")
View Source
var ErrMissingFeatureFlagInCtx = errors.New("template: feature flag config missing in ctx")
View Source
var ErrNotFound = errors.New("requested template not found")
View Source
var ErrUpdateDisallowed = errors.New("template: update disallowed")
View Source
var TranslationJSON = resource.RegisterResource(&translationJSON{})

Functions

func Embed

func Embed(data map[string]any, value any)

Embed embeds the given value into data.

func EnsureTime

func EnsureTime(anyValue any) any

func HTMLAttr

func HTMLAttr(v string) template.HTMLAttr

func IsNil

func IsNil(v any) bool

func LinksToText

func LinksToText(s any) string

LinksToText removes the tags in s.

func MakeTemplateFuncMap

func MakeTemplateFuncMap(t tpl) map[string]any

func RFC3339

func RFC3339(date any) any

func ResolveError

func ResolveError(apiErr *apierrors.APIError, input ResolveErrorRawInput) map[string]any

func SanitizeLinksHTML

func SanitizeLinksHTML(s any) template.HTML

SanitizeLinksHTML keeps http(s) links in s and escapes the rest. s must come from RenderText, not translate, because translate wraps the text in a <span>.

func ShowAttributeValue

func ShowAttributeValue(v any) string

Types

type AGTextTemplate

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

AGTextTemplate is a wrapper of text/template, execpt that <no value> will always be rendered as empty string

func (*AGTextTemplate) Execute

func (t *AGTextTemplate) Execute(wr io.Writer, data any) error

func (*AGTextTemplate) String

func (t *AGTextTemplate) String(templateName string) string

For unit test

func (*AGTextTemplate) Wrap

func (t *AGTextTemplate) Wrap(tpl *texttemplate.Template) error

type DefaultLanguageTag

type DefaultLanguageTag string

type Engine

type Engine struct {
	Resolver EngineTemplateResolver
}

func (*Engine) LevelSpecificTranslation

func (e *Engine) LevelSpecificTranslation(ctx context.Context, level resource.FsLevel, preferredLanguages []string) (*TranslationMap, error)

func (*Engine) Render

func (e *Engine) Render(ctx context.Context, resource Resource, preferredLanguages []string, data any) (*RenderResult, error)

func (*Engine) RenderPublicText

func (e *Engine) RenderPublicText(ctx context.Context, tplStr string, data any) (string, error)

func (*Engine) RenderStatus

func (e *Engine) RenderStatus(w http.ResponseWriter, r *http.Request, status int, tpl Resource, data any)

func (*Engine) Translation

func (e *Engine) Translation(ctx context.Context, preferredLanguages []string) (*TranslationMap, error)

type EngineTemplateResolver

type EngineTemplateResolver interface {
	ResolveHTML(ctx context.Context, desc *HTML, preferredLanguages []string) (*HTMLTemplateEffectiveResource, error)
	ResolveMessageHTML(ctx context.Context, desc *MessageHTML, preferredLanguages []string) (*HTMLTemplateEffectiveResource, error)
	ResolvePlainText(ctx context.Context, desc *PlainText, preferredLanguages []string) (*TextTemplateEffectiveResource, error)
	ResolveMessagePlainText(ctx context.Context, desc *MessagePlainText, preferredLanguages []string) (*TextTemplateEffectiveResource, error)
	ResolveTranslations(ctx context.Context, preferredLanguages []string) (map[string]Translation, error)
	ResolveLevelSpecificTranslations(ctx context.Context, level resource.FsLevel, preferredLanguages []string) (map[string]Translation, error)
}

type FormatTextTemplate

type FormatTextTemplate struct{}

func (FormatTextTemplate) CheckFormat

func (FormatTextTemplate) CheckFormat(ctx context.Context, value any) error

type GetErrorInput

type GetErrorInput struct {
	ByReason   []string `json:"by_reason,omitempty"`
	ByLocation []string `json:"by_location,omitempty"` // this field actually check both location and required field
}

type HTML

type HTML struct {
	// Name is the name of template
	Name string
	// ComponentDependencies is the HTML component templates this template depends on.
	ComponentDependencies []*HTML
}

HTML defines a HTML template that is non-customizable

func RegisterHTML

func RegisterHTML(name string, dependencies ...*HTML) *HTML

func (*HTML) FindResources

func (t *HTML) FindResources(fs resource.Fs) ([]resource.Location, error)

func (*HTML) MatchResource

func (t *HTML) MatchResource(path string) (*resource.Match, bool)

func (*HTML) UpdateResource

func (t *HTML) UpdateResource(_ context.Context, _ []resource.ResourceFile, resrc *resource.ResourceFile, data []byte) (*resource.ResourceFile, error)

func (*HTML) ViewResources

func (t *HTML) ViewResources(ctx context.Context, resources []resource.ResourceFile, view resource.View) (any, error)

type HTMLTemplateEffectiveResource

type HTMLTemplateEffectiveResource struct {
	Data        []byte
	LanguageTag string
	Template    *htmltemplate.Template
}

type LimitWriter

type LimitWriter struct {
	// Writer is the underlying writer.
	Writer io.Writer
	// RemainingQuota is the remaining quota in bytes.
	RemainingQuota int64
}

func NewLimitWriter

func NewLimitWriter(w io.Writer) *LimitWriter

func (*LimitWriter) Write

func (w *LimitWriter) Write(p []byte) (n int, err error)

type MessageHTML

type MessageHTML struct {
	// Name is the name of template
	Name string
}

MessageHTML defines a HTML template that is customizable

func RegisterMessageHTML

func RegisterMessageHTML(name string) *MessageHTML

func (*MessageHTML) FindResources

func (t *MessageHTML) FindResources(fs resource.Fs) ([]resource.Location, error)

func (*MessageHTML) MatchResource

func (t *MessageHTML) MatchResource(path string) (*resource.Match, bool)

func (*MessageHTML) UpdateResource

func (t *MessageHTML) UpdateResource(ctx context.Context, _ []resource.ResourceFile, resrc *resource.ResourceFile, data []byte) (*resource.ResourceFile, error)

func (*MessageHTML) ViewResources

func (t *MessageHTML) ViewResources(ctx context.Context, resources []resource.ResourceFile, view resource.View) (any, error)

type MessagePlainText

type MessagePlainText struct {
	// Name is the name of template
	Name string
}

MessagePlainText defines a plain text template that is customizable

func RegisterMessagePlainText

func RegisterMessagePlainText(name string) *MessagePlainText

func (*MessagePlainText) FindResources

func (t *MessagePlainText) FindResources(fs resource.Fs) ([]resource.Location, error)

func (*MessagePlainText) MatchResource

func (t *MessagePlainText) MatchResource(path string) (*resource.Match, bool)

func (*MessagePlainText) UpdateResource

func (t *MessagePlainText) UpdateResource(ctx context.Context, _ []resource.ResourceFile, resrc *resource.ResourceFile, data []byte) (*resource.ResourceFile, error)

func (*MessagePlainText) ViewResources

func (t *MessagePlainText) ViewResources(ctx context.Context, resources []resource.ResourceFile, view resource.View) (any, error)

type PlainText

type PlainText struct {
	// Name is the name of template
	Name string
	// ComponentDependencies is the plain text component templates this template depends on.
	ComponentDependencies []*PlainText
}

PlainText defines a plain text template that is non-customizable

func RegisterPlainText

func RegisterPlainText(name string, dependencies ...*PlainText) *PlainText

func (*PlainText) FindResources

func (t *PlainText) FindResources(fs resource.Fs) ([]resource.Location, error)

func (*PlainText) MatchResource

func (t *PlainText) MatchResource(path string) (*resource.Match, bool)

func (*PlainText) UpdateResource

func (t *PlainText) UpdateResource(_ context.Context, _ []resource.ResourceFile, resrc *resource.ResourceFile, data []byte) (*resource.ResourceFile, error)

func (*PlainText) ViewResources

func (t *PlainText) ViewResources(ctx context.Context, resources []resource.ResourceFile, view resource.View) (any, error)

type RenderResult

type RenderResult struct {
	String      string
	LanguageTag string
}

type ResolveErrorInput

type ResolveErrorInput map[string]*GetErrorInput

type ResolveErrorRawInput

type ResolveErrorRawInput map[string]any // actually map[string][]string, but go template dict always map[string]interface{}

type Resolver

type Resolver struct {
	Resources             ResourceManager
	DefaultLanguageTag    DefaultLanguageTag
	SupportedLanguageTags SupportedLanguageTags
}

func (*Resolver) ResolveHTML

func (r *Resolver) ResolveHTML(ctx context.Context, desc *HTML, preferredLanguages []string) (*HTMLTemplateEffectiveResource, error)

func (*Resolver) ResolveLevelSpecificTranslations

func (r *Resolver) ResolveLevelSpecificTranslations(ctx context.Context, level resource.FsLevel, preferredLanguages []string) (map[string]Translation, error)

func (*Resolver) ResolveMessageHTML

func (r *Resolver) ResolveMessageHTML(ctx context.Context, desc *MessageHTML, preferredLanguages []string) (*HTMLTemplateEffectiveResource, error)

func (*Resolver) ResolveMessagePlainText

func (r *Resolver) ResolveMessagePlainText(ctx context.Context, desc *MessagePlainText, preferredLanguages []string) (*TextTemplateEffectiveResource, error)

func (*Resolver) ResolvePlainText

func (r *Resolver) ResolvePlainText(ctx context.Context, desc *PlainText, preferredLanguages []string) (*TextTemplateEffectiveResource, error)

func (*Resolver) ResolveTranslations

func (r *Resolver) ResolveTranslations(ctx context.Context, preferredLanguages []string) (map[string]Translation, error)

type Resource

type Resource interface {
	// contains filtered or unexported methods
}

type ResourceManager

type ResourceManager interface {
	Read(ctx context.Context, desc resource.Descriptor, view resource.View) (any, error)
}

type SupportedLanguageTags

type SupportedLanguageTags []string

type TextTemplateEffectiveResource

type TextTemplateEffectiveResource struct {
	Data        []byte
	LanguageTag string
	Template    *texttemplate.Template
}

type Translation

type Translation struct {
	LanguageTag string
	Value       string
}

func (Translation) GetLanguageTag

func (t Translation) GetLanguageTag() string

type TranslationMap

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

func (*TranslationMap) HasKey

func (t *TranslationMap) HasKey(key string) bool

func (*TranslationMap) RenderText

func (t *TranslationMap) RenderText(key string, args any) (string, error)

type Validator

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

func NewValidator

func NewValidator(opts ...ValidatorOption) *Validator

func (*Validator) ValidateHTMLTemplate

func (v *Validator) ValidateHTMLTemplate(template *html.Template) error

func (*Validator) ValidateTextTemplate

func (v *Validator) ValidateTextTemplate(template *text.Template) error

type ValidatorOption

type ValidatorOption func(*Validator)

func AllowDeclaration

func AllowDeclaration(b bool) ValidatorOption

func AllowIdentifierNode

func AllowIdentifierNode(b bool) ValidatorOption

func AllowRangeNode

func AllowRangeNode(b bool) ValidatorOption

func AllowTemplateNode

func AllowTemplateNode(b bool) ValidatorOption

func ForbidIdentifiers

func ForbidIdentifiers(idens []string) ValidatorOption

func MaxDepth

func MaxDepth(d int) ValidatorOption

Jump to

Keyboard shortcuts

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