templating

package module
v0.0.0-...-5832596 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: MIT Imports: 6 Imported by: 0

README

templating

Build Status Go Report Card GoDoc

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddEngine

func AddEngine(engine ...render.Engine)

func Execute

func Execute(ctx context.Context, wr io.Writer, name string, data interface{}, opts ...render.Option) error
Example
package main

import (
	"context"
	html "html/template"
	"log"
	"os"

	"gitoa.ru/go-4devs/templating"
	"gitoa.ru/go-4devs/templating/gohtml"
)

type Data struct {
	Name string
}

func main() {
	ctx := context.Background()
	data := Data{Name: "andrey"}

	gohtml.Must(html.New("example.html").Parse(`Hello {{ .Name }}!`))

	err := templating.Execute(ctx, os.Stdout, "example.html.gohtml", data)
	if err != nil {
		log.Fatalln(err)
	}
}
Output:

Hello andrey!
Example (Text)
package main

import (
	"context"
	"log"
	"os"

	text "text/template"

	"gitoa.ru/go-4devs/templating"
	"gitoa.ru/go-4devs/templating/gotxt"
)

type Data struct {
	Name string
}

func main() {
	ctx := context.Background()
	data := Data{Name: "<u>text</u>"}

	gotxt.Must(text.New("example.txt").Parse(`Hello {{ .Name }}!`))

	err := templating.Execute(ctx, os.Stdout, "example.txt.gotxt", data)
	if err != nil {
		log.Fatalln(err)
	}
}
Output:

Hello <u>text</u>!
Example (WithOption)
package main

import (
	"context"
	html "html/template"
	"log"
	"os"

	"gitoa.ru/go-4devs/templating"
	"gitoa.ru/go-4devs/templating/gohtml"
	"gitoa.ru/go-4devs/templating/render"
)

type Data struct {
	Name string
}

func main() {
	ctx := context.Background()
	data := Data{Name: "<b>world</b>"}

	gohtml.Must(html.New("example_with_option.html").Parse(`Hello <s>{{ .Name }}</s>!`))

	err := templating.Execute(ctx, os.Stdout, "example_with_option.html", data, render.WithEngine(gohtml.Name))
	if err != nil {
		log.Fatalln(err)
	}
}
Output:

Hello <s>&lt;b&gt;world&lt;/b&gt;</s>!
Example (WithoutEngine)
package main

import (
	"context"
	html "html/template"
	"log"
	"os"

	text "text/template"

	"gitoa.ru/go-4devs/templating"
	"gitoa.ru/go-4devs/templating/gohtml"
	"gitoa.ru/go-4devs/templating/gotxt"
)

type Data struct {
	Name string
}

func main() {
	ctx := context.Background()
	data := Data{Name: "engine"}

	gotxt.Must(text.New("example_engine.txt").Parse(`Text {{ .Name }}!`))
	gohtml.Must(html.New("example_engine.html").Parse(`Html {{ .Name }}!`))

	err := templating.Execute(ctx, os.Stdout, "example_engine.html", data)
	if err != nil {
		log.Fatalln(err)
	}
}
Output:

Html engine!

func SetParser

func SetParser(parser render.Parser)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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