package generator

import "github.com/a-h/templ/generator"

Index

Functions

func HasChanged

func HasChanged(previous, updated GeneratorOutput) bool

HasChanged returns true if the generated file should be written to disk, and therefore, also requires a recompilation.

Types

type GenerateOpt

type GenerateOpt func(g *generator) error

func WithFileName

func WithFileName(name string) GenerateOpt

WithFileName sets the filename of the templ file in template rendering error messages.

func WithSkipCodeGeneratedComment

func WithSkipCodeGeneratedComment() GenerateOpt

WithSkipCodeGeneratedComment skips the code generated comment at the top of the file. gopls disables edit related functionality for generated files, so the templ LSP may wish to skip generation of this comment so that gopls provides expected results.

func WithTimestamp

func WithTimestamp(d time.Time) GenerateOpt

WithTimestamp enables the generated date to be included in the generated code.

func WithVersion

func WithVersion(v string) GenerateOpt

WithVersion enables the version to be included in the generated code.

type GeneratorOptions

type GeneratorOptions struct {
	// Version of templ.
	Version string
	// FileName to include in error messages if string expressions return an error.
	FileName string
	// SkipCodeGeneratedComment skips the code generated comment at the top of the file.
	SkipCodeGeneratedComment bool
	// GeneratedDate to include as a comment.
	GeneratedDate string
}

type GeneratorOutput

type GeneratorOutput struct {
	Options   GeneratorOptions  `json:"meta"`
	SourceMap *parser.SourceMap `json:"sourceMap"`
	Literals  []string          `json:"literals"`
}

func Generate

func Generate(template parser.TemplateFile, w io.Writer, opts ...GenerateOpt) (op GeneratorOutput, err error)

Generate generates Go code from the input template file to w, and returns a map of the location of Go expressions in the template to the location of the generated Go code in the output.

type RangeWriter

type RangeWriter struct {
	Current parser.Position

	Literals []string
	// contains filtered or unexported fields
}

func NewRangeWriter

func NewRangeWriter(w io.Writer) *RangeWriter

func (*RangeWriter) Write

func (rw *RangeWriter) Write(s string) (r parser.Range, err error)

func (*RangeWriter) WriteIndent

func (rw *RangeWriter) WriteIndent(level int, s string) (r parser.Range, err error)

func (*RangeWriter) WriteStringLiteral

func (rw *RangeWriter) WriteStringLiteral(level int, s string) (r parser.Range, err error)

Source Files

generator.go rangewriter.go

Directories

PathSynopsis
generator/htmldiff
generator/test-a-href
generator/test-attribute-errors
generator/test-attribute-escaping
generator/test-call
generator/test-cancelled-context
generator/test-complex-attributes
generator/test-constant-attribute-escaping
generator/test-context
generator/test-css-expression
generator/test-css-middleware
generator/test-css-usage
generator/test-doctype
generator/test-doctype-html4
generator/test-element-attributes
generator/test-elseif
generator/test-for
generator/test-form-action
generator/test-go-comments
generator/test-go-template-in-templ
generator/test-html
generator/test-html-comment
generator/test-if
generator/test-ifelse
generator/test-import
generator/test-js-unsafe-usage
generator/test-js-usage
generator/test-method
generator/test-once
generator/test-only-scripts
generator/test-raw-elements
generator/test-script-expressions
generator/test-script-inline
generator/test-script-usage
generator/test-script-usage-nonce
generator/test-spread-attributes
generator/test-string
generator/test-string-errors
generator/test-style-attribute
generator/test-switch
generator/test-switchdefault
generator/test-templ-element
generator/test-templ-in-go-template
generator/test-text
generator/test-text-whitespace
generator/test-void
generator/test-whitespace-around-go-keywords
Version
v0.3.857 (latest)
Published
Mar 25, 2025
Platform
linux/amd64
Imports
15 packages
Last checked
1 week ago

Tools for package owners.