Documentation ¶
Overview ¶
Package tmplstr provides text and html template utilities
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PruneTemplateComments
deprecated
PruneTemplateComments is like RemoveTemplateComments with a very fundamental difference, instead of using ScanCarve and ScanBothCarve, PruneTemplateComments uses ParseTemplate, walks the Variables in the Tree, setting all Variable.Comment values to nil and finally returning the rendered results of the modified Tree. If PruneTemplateComments fails to ParseTemplate, the error is returned and pruned is empty
RemoveTemplateComments is much faster at this task and PruneTemplateComments is more-or-less an example of using ParseTemplate to modify the template source text programmatically
Benchmark Comparison:
| | PruneTemplateComments | RemoveTemplateComments | |----------|-----------------------|---------------------------------| | | sec/op | sec/op vs base | | Comments | 0.037610n ± 0% | 0.005125n ± 16% -86.37% (n=50) | |----------|---------------------------------------------------------| | | (both implementations have 0 B/op and 0 allocs/op) |
Deprecated: please use RemoveTemplateComments instead
func RemoveTemplateComments ¶
RemoveTemplateComments removes all C-style block comments from within template pipelines, preserving escaped and quoted comments, using github.com/go-corelibs/strings ScanCarve and ScanBothCarve making RemoveTemplateComments very fast compared to PruneTemplateComments
Types ¶
type Action ¶
type Action struct { Open *string `parser:"( @StatementOpen " json:"open,omitempty"` Pipelines Pipelines `parser:" @@" json:"pipelines,omitempty"` Close *string `parser:" @StatementClose )" json:"close,omitempty"` }
Action represents a single text or html template action
See: https://pkg.go.dev/text/template#hdr-Actions
func (*Action) WalkVariables ¶
func (a *Action) WalkVariables(fn VariablesWalkFn) (stopped bool)
WalkVariables walks this Action, calling the given VariablesWalkFn for all Variables present
type Branch ¶
type Grouping ¶
type Grouping struct { Open *string `parser:"( @GroupOpen" json:"open,omitempty"` Group *Pipeline `parser:" @@" json:"group,omitempty"` Close *string `parser:" @GroupClose )" json:"close,omitempty"` }
Grouping represents a grouping Pipeline
Example: in `{{ ident (inner pipeline) }}` the Grouping is the `(inner pipeline)` portion
type Pipeline ¶
type Pipeline struct { Root Variables `parser:"@@ ( @@ )*" json:"variables,omitempty"` Pipe *Pipeline `parser:"( Pipe @@ )?" json:"piped,omitempty"` }
Pipeline defines the source text representing a list of Variables which may also be piped into another Pipeline instance
type Tree ¶
type Tree []*Branch
Tree is a list of Branch instances and is the top of the ParseTemplate abstract syntax tree
func ParseTemplate ¶
ParseTemplate uses github.com/alecthomas/participle/v2 to parse the given template file content into an abstract syntax tree. ParseTemplate is intended to facilitate extracting contextual information from text or html template source text
func (Tree) WalkVariables ¶
func (t Tree) WalkVariables(fn VariablesWalkFn) (stopped bool)
WalkVariables walks this Tree, calling the given VariablesWalkFn for all Variables present
type Variable ¶
type Variable struct { Assign *string `parser:"( @Assignment" json:"assign,omitempty"` Range *string `parser:" | @Range" json:"range,omitempty"` Ident *string `parser:" | @Ident" json:"ident,omitempty"` Keyword *string `parser:" | @Keyword" json:"keyword,omitempty"` Literal *string `parser:" | @Literal" json:"literal,omitempty"` String *string `parser:" | @String" json:"string,omitempty"` Rune *string `parser:" | @Rune" json:"rune,omitempty"` Float *float64 `parser:" | @Float" json:"float,omitempty"` Int *int `parser:" | @Int" json:"int,omitempty"` Space *string `parser:" | ( @Space )+" json:"space,omitempty"` Comment *string `parser:" | @Comment" json:"comment,omitempty"` Grouping *Grouping `parser:" | @@ )" json:"grouping,omitempty"` }
type Variables ¶
type Variables []*Variable
type VariablesWalkFn ¶
VariablesWalkFn is the function signature for Variable-walking methods. When these functions return true, the WalkVariables call is immediately stopped