templates

package
v0.0.0-...-7b1ff6f Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2019 License: MIT Imports: 16 Imported by: 0

README

Documentation

Overview

Package templates compiles all templates in a directory tree into a Service that provides a Render method to execute other templates inside its context.

Example

The following example program will load a template directory tree defined as a constant and then render a template file from another directory using the loaded Service into the standard output.

package main

import (
    "os"
    "path"
    "github.com/leonelquinteros/thtml/templates"
)

const (
    _templates = "/path/to/templates"
    _public    = "/path/to/web/root"
)

func main() {
    tplService, err := templates.Load(_templates)
    if err != nil {
        panic(err.Error())
    }

    tplService.Render(os.Stdout, path.Join(_public, "index.html"), nil)
}

Index

Constants

This section is empty.

Variables

View Source
var FuncMap = template.FuncMap{
	"ID":      ID,
	"BuildID": BuildID,
}

FuncMap is passed to the template object that renders every view

Functions

func BuildID

func BuildID() string

BuildID returns a randomly-generated-at-runtime constant value

func ID

func ID() string

ID returns a random [8]byte value encoded as hex string (16).

Types

type EmptyTemplateError

type EmptyTemplateError struct {
	// Error composition
	TError
}

EmptyTemplateError is returned when templates.Service hasn't been loaded.

func NewEmptyTemplateError

func NewEmptyTemplateError() EmptyTemplateError

NewEmptyTemplateError returns a new EmptyTemplateError object

type IError

type IError interface {
	// errors.Error implementation
	Error() string

	// Stringer implementation
	String() string

	// Dump stack trace
	Stack() string

	// Creation time
	Created() time.Time
}

IError is the common error type interface.

type Service

type Service struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Service is the template handler. After Load()'ing a directory tree, will be ready to render any template into its context.

func Load

func Load(dir string, extensions ...string) (*Service, error)

Load creates a new *templates.Service object and loads the templates in the provided directory. Custom set of filename extensions can be supplied

func (*Service) AddExtension

func (s *Service) AddExtension(ext string)

AddExtension adds a new filename extension (i.e. ".txt") to the list of extensions to support. Extensions not supported will be rendered and/or compiled as they are without template parsing. This method is safe to use from multiple/concurrent goroutines.

func (*Service) Build

func (s *Service) Build(in, out string) (err error)

Build compiles all files in the provided directory and outputs the results to the build dir. This method is NOT safe to use from multiple/concurrent goroutines

func (*Service) Load

func (s *Service) Load(dir string) error

Load takes a directory path and loads all templates on it. This method is NOT safe to use from multiple/concurrent goroutines

func (*Service) Minify

func (s *Service) Minify(m bool)

Minify sets the configuration to minify the output

func (*Service) RemoveExtension

func (s *Service) RemoveExtension(ext string)

RemoveExtension deletes a supported filename extension from the list. This method is safe to use from multiple/concurrent goroutines.

func (*Service) Render

func (s *Service) Render(w io.Writer, filename string, data interface{}) error

Render compiles the provided template filename in the loaded templates and writes the output to the provided io.Writer. This method is safe to use from multiple/concurrent goroutines

func (*Service) ValidExtension

func (s *Service) ValidExtension(ext string) bool

ValidExtension returns true if the filename extension provided is supported. This method is safe to use from multiple/concurrent goroutines.

type TError

type TError struct {
	// contains filtered or unexported fields
}

TError templates error base class. Implements IError

func NewError

func NewError(message string) TError

NewError returns a new TError object

func (TError) Created

func (err TError) Created() time.Time

Created time getter

func (TError) Error

func (err TError) Error() string

error implementation

func (TError) Stack

func (err TError) Stack() string

Stack getter

func (TError) String

func (err TError) String() string

Stringer implementation

Jump to

Keyboard shortcuts

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