templatex

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2022 License: MIT Imports: 8 Imported by: 0

README

templatex

The missing function for parsing nested templates in Go, like you know it from Laravel, Django, etc.

  • Use the whole feature set of Go's templating library
  • No new syntax or keywords. Just Go's templating functions in the right order
  • No dependencies, just Go's standard library
  • Super easy to use

Your template structure can now look like this and parsing will be done for you:

templates/
  layout.html

  includes/
    header.html
    footer.html

  dashboard/
    includes/
      widgets.html
    view.html
    edit.html

  profile/
    view.html
    edit.html

    payments/
      methods.html
      add.html

Installation

go get -u github.com/philippta/templatex

Usage

templatex is very easy to use as it only has one type and three methods. The most basic way to use it is with default options. This will parse the templates/ dire

t := templatex.New()

t.ParseDir("templates/")

t.ExecuteTemplate(w, "profile/view", userdata)

The parser also has options to use different names for the includes directory and layout files. Additional template functions can also be supplied:

t := &templatex.Template{
    Layout:     "layout.html",
    IncludeDir: "includes",
    FuncMap: template.FuncMap{
        "uppercase": strings.ToUpper,
    },
}

Parsing order

Based on the example from above:

templates/
  layout.html

  includes/
    header.html
    footer.html

  dashboard/
    includes/
      widgets.html
    view.html
    edit.html

  profile/
    view.html
    edit.html

    payments/
      methods.html
      add.html

templatex will parse the templates in the following order:

dashboard/view:
  templates/includes/header.html
  templates/includes/footer.html
  templates/dashboard/includes/widgets.html
  templates/layout.html
  templates/dashboard/view.html

dashboard/edit:
  templates/includes/header.html
  templates/includes/footer.html
  templates/dashboard/includes/widgets.html
  templates/layout.html
  templates/dashboard/edit.html

profile/view:
  templates/includes/header.html
  templates/includes/footer.html
  templates/layout.html
  templates/profile/view.html

profile/edit:
  templates/includes/header.html
  templates/includes/footer.html
  templates/layout.html
  templates/profile/edit.html

profile/payments/methods:
  templates/includes/header.html
  templates/includes/footer.html
  templates/layout.html
  templates/profile/payments/methods.html

profile/payments/add:
  templates/includes/header.html
  templates/includes/footer.html
  templates/layout.html
  templates/profile/payments/add.html

Defining nested templates

Defining nested templates in Go is relatively simple. There are just three instructions you should keep in mind:

  1. block for creating an new block
  2. define for filling a block
  3. template for rendering a partial/include template
Step 1: Creating a block

Creating a block is the essential part for nested templates, as these parts can be later filled by a child template. It is created by the block command.

<!-- layout.html -->
<body>
    <h1>
        {{block "title" .}}Default Title{{end}}
    </h1>

    {{block "content" .}}
        <p>
            This renders, when the block has not been filled.
            Otherwise this message is discarded.
            Good for default content.
        </p>
    {{end}}
</body>
Step 2: Filling a block

Filling a block which was created in a parent template is done with the define command. So basically, we redefine the contents of a block.

<!-- profile/view.html -->
{{define "title"}}
    You're on profile/view
{{end}}

{{define "content"}}
    <p>
        This will render inside the "content" block.
        There is no need to place any html outside of this block
    </p>
{{end}}
Step 3: Using partial/include templates

Creating an include template is similar to step 2. Here we're not redefining a existing block. We rather define a new block which is not rendered immediately.

<!-- includes/footer.html -->
{{define "footer"}}
    &copy; Company 2020. All rights reserved
{{end}}

Then render it like this with the template command:

<!-- layout.html again -->
...
{{template "footer" .}} <!-- notice: no {{end}} here -->
</body>

Example

Make sure to checkout the example in this repository.

License

MIT

Enjoy :-)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecuteError

type ExecuteError struct {
	Template string
	Err      error
}

ExecuteError represents an error which can occure while trying to execute a template

func (ExecuteError) Error

func (e ExecuteError) Error() string

func (ExecuteError) Unwrap

func (e ExecuteError) Unwrap() error

type NotFoundError

type NotFoundError struct {
	Template string
}

NotFound represents an error which can occure when trying to execute a template, which does not exist

func (NotFoundError) Error

func (e NotFoundError) Error() string

type ParseError

type ParseError struct {
	Path string
	Err  error
}

ParseError represents an error which can occure when trying to parse a template

func (ParseError) Error

func (e ParseError) Error() string

func (ParseError) Unwrap

func (e ParseError) Unwrap() error

type Template

type Template struct {
	// Layout specifies the filename of the layout files in a directory
	// Most commonly: "layout.html" or "base.html"
	Layout string

	// IncludeDir specifies the directory name where partial templates can be found
	// Most commonly: "includes", "include" or "inc"
	IncludeDir string

	// FuncMap is a map of functions, given to the templates while parsing
	FuncMap template.FuncMap
	// contains filtered or unexported fields
}

Template represents a container for multiple templates parsed from a directory

func New

func New() *Template

New creates a new Template with sane default values for directories like: templates/

layout.html

includes/
  header.html
  footer.html

profile/
  view.html
  edit.html

func (*Template) ExecuteTemplate

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

ExecuteTemplate executes a template by its name to a io.Writer with any given data

func (*Template) ParseDir

func (t *Template) ParseDir(dir string) (err error)

ParseDir parses all templates inside a given directory

func (*Template) ParseFS

func (t *Template) ParseFS(files fs.FS, dir string) (err error)

ParseFS parses all templates inside a given fs.FS

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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