goim: github.com/BurntSushi/goim/tpl Index | Files

package tpl

import "github.com/BurntSushi/goim/tpl"

Package tpl provides convenience functions that are loaded into every Goim template, along with some functions for parsing and executing Goim templates. This package also defines a few key types, like Args, that describe all values passed to a template when executed.

In general, every template executed targets one specific entity, which is set in the E field of the Args struct. Some templates require additional information, which is set in the A field of the Args struct.

This package also uses global state to set the database to use in some of the helper functions defined. This is unfortunate but convenient. If SetDB is not called and a helper function is used that requires it, executing the template will return an error.


Please consider this package as beta material. I am reasonably happy with what is here so far and I don't expect to change it. But it hasn't been used much, so I'd like to wait before declaring it stable.


Package Files

default.go doc.go helpers.go template.go


var Defaults string = defaults

Defaults is a string containing the default templates. It corresponds precisely to the content in command.tpl.

var Functions = template.FuncMap{
    "lines":      lines,
    "wrap":       wrap,
    "underlined": underlined,

    "count_seasons":  countSeasons,
    "count_episodes": countEpisodes,
    "tvshow":         tvshow,

    "running_times":      attrGetter(new(imdb.RunningTimes)),
    "release_dates":      attrGetter(new(imdb.ReleaseDates)),
    "aka_titles":         attrGetter(new(imdb.AkaTitles)),
    "alternate_versions": attrGetter(new(imdb.AlternateVersions)),
    "color_info":         attrGetter(new(imdb.ColorInfos)),
    "mpaa":               attrGetter(new(imdb.RatingReason)),
    "sound_mixes":        attrGetter(new(imdb.SoundMixes)),
    "taglines":           attrGetter(new(imdb.Taglines)),
    "trivia":             attrGetter(new(imdb.Trivias)),
    "goofs":              attrGetter(new(imdb.Goofs)),
    "genres":             attrGetter(new(imdb.Genres)),
    "languages":          attrGetter(new(imdb.Languages)),
    "literature":         attrGetter(new(imdb.Literatures)),
    "locations":          attrGetter(new(imdb.Locations)),
    "links":              attrGetter(new(imdb.Links)),
    "plots":              attrGetter(new(imdb.Plots)),
    "quotes":             attrGetter(new(imdb.Quotes)),
    "rank":               attrGetter(new(imdb.UserRank)),
    "credits":            attrGetter(new(imdb.Credits)),

    "eq": func(a, b interface{}) bool { return a == b },
    "ne": func(a, b interface{}) bool { return a != b },

    "lt": func(a, b int) bool { return a < b },
    "le": func(a, b int) bool { return a <= b },
    "gt": func(a, b int) bool { return a > b },
    "ge": func(a, b int) bool { return a >= b },

    "ltf": func(a, b float64) bool { return a < b },
    "lef": func(a, b float64) bool { return a <= b },
    "gtf": func(a, b float64) bool { return a > b },
    "gef": func(a, b float64) bool { return a >= b },

Functions corresponds to a map of functions that is available in every Goim template.

The "lines" function takes a string and returns a list of strings corresponding to each line.

The "wrap" function takes a column limit and a string and wraps the string to the given number of columns.

The "underlined" function takes a string to repeat and a string to underline, and returns the string to underline, followed by a new line, followed by the string to repeat N times, where N is the length of the string to underline.

The "count_seasons" function takes one parameter that is a TV show and returns the number of seasons that have aired.

The "count_episodes" function takes one parameter that is a TV show and returns the number of episodes that have aired.

The "tvshow" function takes one parameter that is an episode and returns its corresponding TV show.

The list of functions starting with "running_times" retrieve attribute values given an entity. All functions accept one argument that must satisfy the imdb.Entity interface and return a list of attribute values.

func ExecText Uses

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

ExecText performs a standard template exec, except it does some post-processing on the output to control whitespace. Namely, 3 or more consecutive new line characters (LF) are replaced with 2 new line characters (LF).

func ParseText Uses

func ParseText(fpath string) (*template.Template, error)

ParseText will open the given file path and parse its contents as a template. If fpath has length 0, then a default set of templates is used.

Note that ParseText does some pre-processing on the template text as a way to reasonably control whitespace. Namely, lines ending with '}}' are completely ignored. Lines ending with '}}\' are not ignored. The template is parsed before these changes are made so that accurate line numbers can be given in case there is an error.

func SetDB Uses

func SetDB(db *imdb.DB)

SetDB should be called by clients of this package to set the database to be used to query information.

This unfortunately relies on global state, but this makes it more convenient and simpler to write templates.

type Args Uses

type Args struct {
    // E is a value that satisfies the imdb.Entity interface, except when
    // showing search results where it corresponds to a search.Result value.
    E   interface{}

    // Attrs is a map of attributes that is template specific. For example,
    // the "search_result" template is given an index that represents the
    // position of the result in the search results.
    A   Attrs

Args is the value that is passed to all templates.

type Attrs Uses

type Attrs map[string]interface{}

Attrs represents a template-specific map of attributes.

Package tpl imports 13 packages (graph) and is imported by 1 packages. Updated 2016-07-18. Refresh now. Tools for package owners.