package module
Version: v0.0.0-...-bb52808 Latest Latest

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

Go to latest
Published: Nov 8, 2017 License: Apache-2.0 Imports: 15 Imported by: 0


Build Status Go Report Card GoDoc Coverage License


All you need to know right now is that chew is a CLI for Go text/template engine with some additional bits and pieces. In chew you decide which templates will be generated and with which data. A more elaborate readme will be written when the time comes.. :)



Package chew is the core package in Chew.

Chew is a lean and fast CLI wrapper for Go text/template with additional functions. The template generation in Chew is data-centric, meaning that the input data dictates which templates will be generated and with which data.

You can define a folder where you store all of your templates and Chew will parse all files in the folder and sub-folders when you run it. When running chew you also define the input data in JSON format, which will be used to generate the output.

For example if we have the folder /templates:

▾ templates
  ▾ special

Content of fidget_spinner.tmpl:

Spinner model: {{ .model }}
Spinner year of construction: {{ .construction.year }}

{{ indentTemplate "case" .case . 2 }}

Content of case.tmpl:

Case type: {{ .parent.model }}.{{ .type }}
Main Bearings:
{{ plugins .bearings "template" "main" . 2 }}

Outer Bearings:
{{ plugins .bearings "template" "outer" . 2 }}

Content of bearing.tmpl:




This section is empty.


View Source
var (
	// Version of Chew
	Version string
	// ReleaseDate of Chew
	ReleaseDate string


func Exists

func Exists(data map[string]interface{}, key string) bool

Exists returns true if field exists in a map, else false.

func Indent

func Indent(identSize int, a string) string

Indent prepends spaces to every line in a multi-line string.

func MaxLength

func MaxLength(data interface{}) int

MaxLength returns the length of the longest field in a []string.

func Offset

func Offset(length int, a interface{}) string

Offset returns a string of blank spaces so that the input string reaches the input length.

func ToMap

func ToMap(data interface{}) (map[string]interface{}, error)

ToMap takes an object and extracts a map[string]interface{}. If the object is already a map[string]interface{} it is returned as it is. If the object is a struct, then the fields of the struct are mapped to a map where the keys are the names of the fields, while the values are the actual values. If anything else is passed to the function an error is thrown.


type Chewable

type Chewable struct {
	Global map[string]interface{}
	Data   []ChewableData

Chewable is the main object which carries the data in chew. It stores everything that is used when generating the data. The slice of ChewableData is looped through and used in executing the specified templates. Chewable also carries a map of objects called Global, which is accessible in every template.

func (*Chewable) UnmarshalJSON

func (c *Chewable) UnmarshalJSON(data []byte) error

UnmarshalJSON parses data from JSON into Chewable. Returns an error if parsing was unsuccessful, else nil.

type ChewableData

type ChewableData struct {
	Templates map[string]string
	Local     map[string]interface{}

ChewableData is a collection of data which can be used in executing one or more templates. The field Templates stores a map in which the key denotes the name of the template which will be generated and the value denotes the output filename. Everything in field Local will be accessible in the templates. If a key in the map Local also exists in the map Global in Chewable, the Local value will be used.

type MultiFileWriter

type MultiFileWriter struct {
	Out string

MultiFileWriter is a Writer which writes everything to files in the folder Out. If the folder defined in Out doesn't exist it is created before writing to the first file. SetOut has to be called before starting to write to this Writer, so that the file is created or truncated before writing to it.

func (*MultiFileWriter) SetOut

func (w *MultiFileWriter) SetOut(filename string)

SetOut sets the filename of the output file into which the succeeding calls to Write will output the content. A file with the provided filename will be created in the folder defined in Out. If the file exists, it will be truncated.

type Template

type Template struct {
	Functions funcmap.Functions
	// contains filtered or unexported fields

Template wraps *text/template.Template and adds some additional functionality. It should be created via chew.New. If a manual instantiation is used the method InjectFunctions should be called before processing templates to be able to use all custom chew functions.

func New

func New(name string) *Template

New creates a new chew.Template with the provided name and injects the template functions.

func (*Template) ExecuteChewable

func (ct *Template) ExecuteChewable(w Writer, c Chewable) error

ExecuteChewable loops through all ChewableData in Chewable and executes every Template defined in ChewableData.Templates. The output is written to the supplied chew.Writer, which also gets notified about the desired output filename before every template execution. If template.Template.ExecuteTemplate returns an error the execution stops and returns it.

dataRaw, _ := ioutil.ReadFile("test/data/example.json")

chewable := &Chewable{}
err := json.Unmarshal(dataRaw, chewable)
if err != nil {

template := New("main")
template.ExecuteChewable(WriterWrapper{os.Stdout}, *chewable)

func (*Template) IndentTemplate

func (ct *Template) IndentTemplate(template string, data interface{}, parent interface{}, indentSize int) string

IndentTemplate is similar to the built-in template function with the additional functionality of indenting the content of the template and accessing parent data. It takes the name of the template, the data which will be sent to the template when executing it, the parent data which will be accessible in the executed template and the number of spaces which will indent the content of the processed template.

func (*Template) IndentTemplates

func (ct *Template) IndentTemplates(nestedTemplates interface{}, templateField string, parent interface{}, indentSize int) string

IndentTemplates is similar to IndentTemplate, only that it processes all templates defined in a slice. It takes the slice of objects which carry the data about the nested templates, the field in the nested template object which carries the name of the template, the parent data which will be accessible in the executed template and the number of spaces which will indent the content of the processed template.

func (*Template) InjectFunctions

func (ct *Template) InjectFunctions()

InjectFunctions adds template specific functions to Template.Functions and adds the function map to the template.

func (*Template) ParseFolder

func (ct *Template) ParseFolder(folderPath string) (*Template, error)

ParseFolder recursively walks through the provided folder path and parses every template it can find with the template suffix.

func (*Template) Plugins

func (ct *Template) Plugins(pluginsRaw interface{}, insertPoint, templateField string, parent interface{}, indentSize int) string

Plugins is similar to IndentTemplates, only that it skips nested templates which don't define a template for the insertion point. It takes the slice of objects which carry the data about the plugins, the insertion point where the plugin will be inserted, the field in the plugin object which carries the name of the template, the parent data which will be accessible in the executed template and the number of spaces which will indent the content of the processed template.

type Writer

type Writer interface {
	// SetOut sets the output filename for the current template
	SetOut(filename string)

Writer extends the io.Writer and adds the option to set the output filename.

When processing Chewable the SetOut method will be called before each template execution informing the writer of the new target file. When writing to standard output this information is meaningless.

type WriterWrapper

type WriterWrapper struct {

WriterWrapper is a convenience object to allow wrapping an io.Writer and implement the Writer interface. The SetOut method is empty and does nothing.

func (WriterWrapper) SetOut

func (WriterWrapper) SetOut(filename string)

SetOut is empty and does nothing.


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL