plantillas

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: LGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Funciones para procesar plantillas y convertirlas en HTML, PDF o EMAIL

Las plantillas soportan la sintaxis estándar de GO y además los siguientes attributos tomados de ThymeLeaf:

  • th:if="condition" => {{ if condition }} T {{ end }}
  • th:each="items" => {{ range items }} T {{ end }}
  • th:with="item" => {{ with item }} T {{end}}
  • th:remove="all" => elimina el tag
  • th:text="content" => reemplaza el contenido del tag
  • th:attr="value" => reemplaza el valor del atributo

Se soportan las funciones de formato DATETIME, DATE, TIME, PRICE y BR.

Ejemplo de plantillla en https://github.com/horus-es/go-util/blob/main/plantillas/template_test.html

Ejemplos de plantillas preparadas para mailing en https://postmarkapp.com/transactional-email-templates

Ejemplos de uso de los atributos de Thymeleaf en https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateXhtmlPdf

func GenerateXhtmlPdf(name, xhtml string, datos any, assets string, ff parse.FormatoFecha, fp parse.FormatoPrecio, out string, opciones ...string) error

Genera un fichero PDF a partir de una plantilla XHTML usando la utilidad wkhtmltopdf, que debe estar previamente instalada. Parámetros:

  • name: nombre arbitrario para la plantilla que aparece en los mensajes de error.
  • xhtml: plantilla en formato XHTML
  • datos: estructura de datos para fusionar con la plantilla
  • assets: ruta de imágenes u otros recursos (attributos src y href). Si es una ruta local, debe estar precedida por file://
  • ff: formato de las fechas para las funciones DATETIME y DATE
  • fp: formato de los precios para la funcion PRICE
  • out: fichero PDF de salida
  • opciones: opciones adicionales utilidad wkhtmltopdf (ver https://wkhtmltopdf.org/usage/wkhtmltopdf.txt)
Example
// Carga plantilla HTML
plantilla, err := os.ReadFile("plantilla.html")
errores.PanicIfError(err)
// Genera fichero PDF
err = GenerateXhtmlPdf(
	"pdf",
	string(plantilla),
	factura,
	"file:///assets",
	parse.DMA,
	parse.EUR,
	"fichero.pdf",
	"--no-outline",
)
errores.PanicIfError(err)
Output:

func MergeXhtmlTemplate

func MergeXhtmlTemplate(name, xhtml string, datos any, assets string, ff parse.FormatoFecha, fp parse.FormatoPrecio) (string, error)

Fusiona una plantilla XHTML con un struct o map de datos.

  • name: nombre arbitrario para la plantilla que aparece en los mensajes de error
  • xhtml: plantilla en formato XHTML
  • datos: estructura de datos para fusionar con la plantilla
  • assets: ruta de imágenes u otros recursos (attributos src y href)
  • ff: formato de las fechas para las funciones DATETIME y DATE
  • fp: formato de los precios para la funcion PRICE
Example
// Cargar plantilla
plantilla, err := os.ReadFile("plantilla.html")
errores.PanicIfError(err)
// Fusionar plantilla con estructura factura
f, err := MergeXhtmlTemplate(
	"html",
	string(plantilla),
	factura,
	"/assets",
	parse.DMA,
	parse.EUR,
)
errores.PanicIfError(err)
// Guardar salida
os.WriteFile("pagina.html", []byte(f), 0666)
Output:

func SendXhtmlMail

func SendXhtmlMail(name, xhtml string, datos any, assets string, ff parse.FormatoFecha, fp parse.FormatoPrecio, adjuntos []string,
	from, to, subject string, bcc, replyto []string,
	host string, port int, username, password string) error

Envia un correo a partir de una plantilla XHTML. Parámetros:

  • name: nombre arbitrario para la plantilla que aparece en los mensajes de error
  • xhtml: plantilla en formato XHTML
  • datos: estructura de datos para fusionar con la plantilla
  • assets: URL de imágenes u otros recursos (attributos src y href). Debe ser una ruta públicamente accesible por internet.
  • ff: formato de las fechas para las funciones DATETIME y DATE
  • fp: formato de los precios para la funcion PRICE
  • adjuntos: ficheros a adjuntar
  • to,form,subject,bcc,replyto: parámetros MIME
  • host,port,username,password: parámtros SMTP. La contraseña debe ir codificada en base64.
Example
// Carga plantilla HTML
plantilla, err := os.ReadFile("plantilla.html")
errores.PanicIfError(err)
// Enviar por correo
err = SendXhtmlMail(
	"mail",
	string(plantilla),
	factura,
	"https://spark2.horus.es/assets",
	parse.DMA,
	parse.EUR,
	[]string{"adjunto.pdf"},
	"remitente@horus.es",
	"destinatario@horus.es",
	"Asunto",
	[]string{"bcc@horus.es"},
	[]string{"replyto@horus.es"},
	"smtp.horus.es",
	25,
	"automaticos@horus.es",
	"secreto",
)
errores.PanicIfError(err)
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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