Template

Variables

var ERROR_CLASS = "hasError"
var (
    // The functions available for use in the templates.
    TemplateFuncs = map[string]interface{}{
        "url": ReverseUrl,
        "eq":  Equal,
        "set": func(renderArgs map[string]interface{}, key string, value interface{}) template.HTML {
            renderArgs[key] = value
            return template.HTML("")
        },
        "append": func(renderArgs map[string]interface{}, key string, value interface{}) template.HTML {
            if renderArgs[key] == nil {
                renderArgs[key] = []interface{}{value}
            } else {
                renderArgs[key] = append(renderArgs[key].([]interface{}), value)
            }
            return template.HTML("")
        },
        "field": NewField,
        "option": func(f *Field, val, label string) template.HTML {
            selected := ""
            if f.Flash() == val {
                selected = " selected"
            }
            return template.HTML(fmt.Sprintf(`<option value="%s"%s>%s</option>`,
                html.EscapeString(val), selected, html.EscapeString(label)))
        },
        "radio": func(f *Field, val string) template.HTML {
            checked := ""
            if f.Flash() == val {
                checked = " checked"
            }
            return template.HTML(fmt.Sprintf(`<input type="radio" name="%s" value="%s"%s>`,
                html.EscapeString(f.Name), html.EscapeString(val), checked))
        },
        "checkbox": func(f *Field, val string) template.HTML {
            checked := ""
            if f.Flash() == val {
                checked = " checked"
            }
            return template.HTML(fmt.Sprintf(`<input type="checkbox" name="%s" value="%s"%s>`,
                html.EscapeString(f.Name), html.EscapeString(val), checked))
        },

        "pad": func(str string, width int) template.HTML {
            if len(str) >= width {
                return template.HTML(html.EscapeString(str))
            }
            return template.HTML(html.EscapeString(str) + strings.Repeat("&nbsp;", width-len(str)))
        },

        "errorClass": func(name string, renderArgs map[string]interface{}) template.HTML {
            errorMap, ok := renderArgs["errors"].(map[string]*ValidationError)
            if !ok || errorMap == nil {
                WARN.Println("Called 'errorClass' without 'errors' in the render args.")
                return template.HTML("")
            }
            valError, ok := errorMap[name]
            if !ok || valError == nil {
                return template.HTML("")
            }
            return template.HTML(ERROR_CLASS)
        },

        "msg": func(renderArgs map[string]interface{}, message string, args ...interface{}) template.HTML {
            return template.HTML(Message(renderArgs[CurrentLocaleRenderArg].(string), message, args...))
        },

        "nl2br": func(text string) template.HTML {
            return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
        },

        "raw": func(text string) template.HTML {
            return template.HTML(text)
        },

        "pluralize": func(items interface{}, pluralOverrides ...string) string {
            singular, plural := "", "s"
            if len(pluralOverrides) >= 1 {
                singular = pluralOverrides[0]
                if len(pluralOverrides) == 2 {
                    plural = pluralOverrides[1]
                }
            }

            switch v := reflect.ValueOf(items); v.Kind() {
            case reflect.Int:
                if items.(int) != 1 {
                    return plural
                }
            case reflect.Slice:
                if v.Len() != 1 {
                    return plural
                }
            default:
                ERROR.Println("pluralize: unexpected type: ", v)
            }
            return singular
        },

        "date": func(date time.Time) string {
            return date.Format(DateFormat)
        },
        "datetime": func(date time.Time) string {
            return date.Format(DateTimeFormat)
        },
        "slug": Slug,
    }
)

func ReverseUrl

func ReverseUrl(args ...interface{}) (string, error)

Return a url capable of invoking a given controller method: "Application.ShowApp 123" => "/app/123"

func Slug

func Slug(text string) string

type GoTemplate

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

Adapter for Go Templates.

func (GoTemplate) Content

func (gotmpl GoTemplate) Content() []string

func (GoTemplate) Render

func (gotmpl GoTemplate) Render(wr io.Writer, arg interface{}) error

return a 'revel.Template' from Go's template.

type Template

type Template interface {
    Name() string
    Content() []string
    Render(wr io.Writer, arg interface{}) error
}

type TemplateLoader

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

This object handles loading and parsing of templates. Everything below the application's views directory is treated as a template.

func NewTemplateLoader

func NewTemplateLoader(paths []string) *TemplateLoader

func (*TemplateLoader) Refresh

func (loader *TemplateLoader) Refresh() *Error

This scans the views directory and parses all templates as Go Templates. If a template fails to parse, the error is set on the loader. (It's awkward to refresh a single Go Template)

func (*TemplateLoader) Template

func (loader *TemplateLoader) Template(name string) (Template, error)

Return the Template with the given name. The name is the template's path relative to a template loader root.

An Error is returned if there was any problem with any of the templates. (In this case, if a template is returned, it may still be usable.)

func (*TemplateLoader) WatchDir

func (loader *TemplateLoader) WatchDir(info os.FileInfo) bool

func (*TemplateLoader) WatchFile

func (loader *TemplateLoader) WatchFile(basename string) bool