gotemplater

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2020 License: MIT Imports: 11 Imported by: 0

README

Go Templater

A simple Golang Template module with auto reload and tree like file layout.

Usage

By default Go Templater will read from "view" directory and will output it's build to "cache/view" but can be overriden in the config. You can also disable the Hot Reload feature in the config.

Here is the directory structure for the view :

view/
    -home/
        -deep.html
    -part/
        -greetings.html
        -who.html
    -home.html

And here is the minimum working code to use Go Templater :

package main

import (
	"log"

	gotemplater "github.com/firmanmm/go-templater"
)

func main() {
	config := gotemplater.NewConfig()
	templater := gotemplater.NewTemplater(config)
	templater.Run()

	res, err := templater.RenderToString("home.html", map[string]interface{}{
		"message": "Hello!",
		"who":     "A Message",
	})
	if err != nil {
		log.Fatalln(err.Error())
	}
	log.Println(res)

	res, err = templater.RenderToString("home.deep.html", map[string]interface{}{
		"message": "Hello!",
		"who":     "A Deeper Message",
	})
	if err != nil {
		log.Fatalln(err.Error())
	}
	log.Println(res)
}

Example

For more information please see Example.

Other Variant

There is also other variant of Go Templater that works with Gin Web Framework, you can find it HERE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	AutoReload bool
	InputDir   string
	OutputDir  string
	FuncMap    template.FuncMap
}

Config hold configuration for Templater

func NewConfig

func NewConfig() *Config

NewConfig return a new instance of Config with following properties AutoReload set to true InputDir set to view OutputDir set to cache/view

type Templater

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

Templater instance to perform hot reload on Go instance

func NewTemplater

func NewTemplater(conf *Config) *Templater

NewTemplater will return a new and ready to use Templater instance

func (*Templater) Render

func (t *Templater) Render(wr io.Writer, name string, data interface{}) error

func (*Templater) RenderToByteArray

func (t *Templater) RenderToByteArray(name string, data interface{}) ([]byte, error)

func (*Templater) RenderToString

func (t *Templater) RenderToString(name string, data interface{}) (string, error)

func (*Templater) Run

func (t *Templater) Run()

func (*Templater) Stop

func (t *Templater) Stop()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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