tmpl2xml

package module
v0.0.0-...-35620d3 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

README

Documentation

tmpl2xml

I find Go templates hard to read and debug because it prioritizes text layout over logic.

This utility allows to convert them to XML which funnily makes it more readable by reprioritizing logic over text layout.

CLI

Install
go install github.com/tiborvass/tmpl2xml/cmd/tmpl2xml@latest
Usage
$ cat go_template.tmpl
Hello {{if .Cond}}world{{else}}friend{{end}}!
$ tmpl2xml go_template.tmpl
<defines>
  <define>
    <text>"Hello "</text>
    <if cond=".Cond">
      <then>
        <text>"world"</text>
      </then>
      <else>
        <text>"friend"</text>
      </else>
    </if>
    <text>"!\n"</text>
  </define>
</defines>

Library

$ go get github.com/tiborvass/tmpl2xml
package main

import (
	"fmt"

	"github.com/tiborvass/tmpl2xml"
)

func main () {
	out, err := tmpl2xml.String("Hello {{if .Cond}}world{{else}}friend{{end}}!")
	if err != nil {
		panic(err)
	}
	fmt.Println(out)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func String

func String(text string) (string, error)

Types

type Converter

type Converter struct {
	Encoder *xml.Encoder
	io.Writer

	EscapeMode EscapeMode
	// contains filtered or unexported fields
}

func (*Converter) FromString

func (c *Converter) FromString(text string) error

func (*Converter) FromTemplate

func (c *Converter) FromTemplate(t *template.Template, text string) error

func (*Converter) FromTree

func (c *Converter) FromTree(tr *parse.Tree, text string) error

type EscapeMode

type EscapeMode uint8
const (
	// EscapeModeQuote uses strconv.Quote to escape text
	EscapeModeQuote EscapeMode = iota
	// EscapeModeXML uses xml.EscapeText to escape text
	EscapeModeXML
)

Directories

Path Synopsis
cmd
tmpl2xml command