template

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

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

Go to latest
Published: Apr 6, 2017 License: MIT Imports: 11 Imported by: 0

README

Go Report Card Build Status

Template

This is a simple wrapper around html/template. The main benefit is that it allows reloading of assets without having to rebuild the binary in debug mode.

This library uses build tags to control debug v.s. nondebug mode. To build with with debug mode turned on use:

$ go build -tags="debug" 

In addition these additional helper functions have been added by default to the system:


Command Description
buildJSFiles Concatenates the given file list, the list is expected to be in a comma separated string, into a file and returns the new file's name.
buildLinkToJSFiles Same as the buildJSFiles but will return a script tag contain the appropriate URL.
buildCSSFiles Concatenates the given file list, the list is expected to be in a comma separated string, into a file and returns the new file's name.
buildLinkToCSSFiles Same as the buildCSSFiles but will return a link tag contain the appropriate URL.

Look at examples/parsefilemin for an example of how to use the package.

package main

import (
   "os"

   "github.com/gdey/template"
   "github.com/tdewolff/minify"
   "github.com/tdewolff/minify/css"
   "github.com/tdewolff/minify/js"
)

func main() {
   min := minify.New()
   min.AddFunc(helpers.JSMimeType, js.Minify)
   min.AddFunc(helpers.CSSMimeType, css.Minify)
   tpl := template.Must(
   	template.Must(template.New("main.template",
   		template.ParseFileList("tpl/parsefile.txt"),
   		template.URLBase("static"),
   		template.DistRoot("examples/tmp"),
   		template.Minifier(helpers.JSMimeType, min),
           template.Minifier(helpers.CSSMimeType, min),
   	)).ParseFiles())
   if err := tpl.Execute(os.Stdout, "No Data"); err != nil {
   	panic(err)
   }
}

/*
output:
<html>
<head>


   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">


<script type="text/javascript" src="static/jsbuild-0b96c8ce82fd02cfea234be7618cd86358b7119e.js"></script>

</head>
<body>
</body>

*/


Documentation

Index

Constants

View Source
const (
	// SrcFileList is a file that contains a list of ParseFiles.
	SrcFileList = sourceType(iota)
	// SrcParseFile is just a ParseFile
	SrcParseFile
	// SrcGlobFile is a glob that may or may not result in ParseFiles.
	SrcGlobFile
)

Variables

View Source
var DefaultBase string

DefaultBase is the default base directory for finding resources.

Functions

func DistRoot

func DistRoot(dist string) anOption

DistRoot is the directory that the build files are going to get written to.

func Helpers

func Helpers(helpers ...template.FuncMap) anOption

Helpers allow you add helper methods to the template. If the value of the map is not a function that can be accepted or the name of the function is not something that can be a function name, this method will panic.

func Minifier

func Minifier(mimetype string, minifier helpers.Minifier) anOption

Minifier to use for the given mimetype. Only one minifier is allowed per mimetype.

func ParseFile

func ParseFile(files ...string) anOption

ParseFile will add the file provided to the set of files to parse for the template.

func ParseFileList

func ParseFileList(ffile string, files ...string) anOption

ParseFileList will add the files from one or more file lists to the set of files to parse for the template. File are reparsed in debug more for each execute statement.

func ParseGlob

func ParseGlob(globs ...string) anOption

ParseGlob will add files it finds from the provided globs to the list of files to parse for the template.

func ResourceRoot

func ResourceRoot(base string) anOption

ResourceRoot sets the base directory to use when resolving any resource.

func URLBase

func URLBase(root string) anOption

URLBase set the base of the url that is generated by the LinkTo* helper functions.

Types

type BConfig

type BConfig []anOption

BConfig is the main object that holds the config for BaseConfig.

func BaseConfig

func BaseConfig(options ...anOption) BConfig

BaseConfig creates a base config that can be used to create other templates from it without have to respecify the options.

func (BConfig) NewTemplate

func (bc BConfig) NewTemplate(name string, options ...anOption) (*Template, error)

NewTemplate returns a template object based on the options set in the base config.

type Template

type Template struct {
	*template.Template
	// contains filtered or unexported fields
}

Template is the main template object.

func Must

func Must(t *Template, err error) *Template

Must will panic if there is an error other returns the template.

func New

func New(name string, options ...anOption) (*Template, error)

New creates a new template. Name of the template and a set of options

func (*Template) BuildCSSFile

func (t *Template) BuildCSSFile(fnames string) (filename string, err error)

BuildCSSFile is a helper function that takes a set of filename and generated a combined (minimizied if a minimizer is provided) Javascript file.

func (*Template) BuildJSFile

func (t *Template) BuildJSFile(fnames string) (filename string, err error)

BuildJSFile is a helper function that takes a set of filename and generated a combined (minimizied if a minimizer is provided) Javascript file.

func (*Template) BuildMimeTypeFile

func (t *Template) BuildMimeTypeFile(mimetype string, fnames string) (filename string, err error)

BuildMimeTypeFile is a helper function that takes a MimeType and a set of filenames and generated a combined (minimizied if a minimizer is provided) file.

func (*Template) Execute

func (t *Template) Execute(w io.Writer, data interface{}) error

Execute will reparse all the template, then execute the template with the given data.

func (*Template) LinkToAndBuildCSSFile

func (t *Template) LinkToAndBuildCSSFile(fnames string) (template.HTML, error)

LinkToAndBuildCSSFile is the same as the buildCSSFiles but will return a link tag contain the appropriate URL.

func (*Template) LinkToAndBuildJSFile

func (t *Template) LinkToAndBuildJSFile(fnames string) (template.HTML, error)

LinkToAndBuildJSFile is the same as the buildJSFiles but will return a script tag contain the appropriate URL.

func (*Template) ParseFiles

func (t *Template) ParseFiles() (*Template, error)

ParseFiles will parse the files that have been build up

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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