providers

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 17 Imported by: 2

README

xtemplate Dot Providers

Dot Providers is how xtemplate users can customize the template dot value {{.}} to access dynamic functionality from within your templates.

This directory contains optional Dot Provider implementations created for and maintained with xtemplate.

[!NOTE]

Users can implement and add their own dot providers by implementing the xtemplate.DotProvider interface and configuring xtemplate to use it.

[!NOTE]

xtemplate also exposes the Config.FuncMaps

Providers

KVDot

Add simple key-value string pairs to your templates. Could be used for runtime config options for your templates.

SqlDot

Connect to a database with any available go driver by its name to run queries and execute procedures against your database from within templates.

FSDot

Open a directory to list and read files with templates.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithDB

func WithDB(name string, db *sql.DB, opt *sql.TxOptions) xtemplate.Option

func WithFS

func WithFS(name string, fs fs.FS) xtemplate.Option

WithFS creates an xtemplate.Option that can be used with xtemplate.Config.Server, xtemplate.Config.Instance, or xtemplate.Main to add an fs dot provider to the config.

func WithKV

func WithKV(name string, kv map[string]string) xtemplate.Option

Types

type DotDB

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

DotDB is used to create a dot field value that can query a SQL database. When any of its statement executing methods are called, it creates a new transaction. When template execution finishes, if there were no errors it automatically commits any uncommitted transactions remaining after template execution completes, but if there were errors then it calls rollback on the transaction.

func (*DotDB) Commit

func (c *DotDB) Commit() (string, error)

Commit manually commits any implicit transactions opened by this DotDB. This is called automatically if there were no errors at the end of template execution.

func (*DotDB) Exec

func (c *DotDB) Exec(query string, params ...any) (result sql.Result, err error)

Exec executes a statement with parameters and returns the raw sql.Result. Note: this can be a bit difficult to use inside a template, consider using other methods that provide easier to use return values.

func (*DotDB) QueryRow

func (c *DotDB) QueryRow(query string, params ...any) (map[string]any, error)

QueryRow executes a query, which must return one row, and returns it as a map[string]any.

func (*DotDB) QueryRows

func (c *DotDB) QueryRows(query string, params ...any) (rows []map[string]any, err error)

QueryRows executes a query and buffers all rows into a []map[string]any object.

func (*DotDB) QueryVal

func (c *DotDB) QueryVal(query string, params ...any) (any, error)

QueryVal executes a query, which must return one row with one column, and returns the value of the column.

func (*DotDB) Rollback

func (c *DotDB) Rollback() (string, error)

Rollback manually rolls back any implicit tranactions opened by this DotDB. This is called automatically if there were any errors that occurred during template exeuction.

type DotDBProvider

type DotDBProvider struct {
	*sql.DB        `json:"-"`
	*sql.TxOptions `json:"-"`
	Driver         string `json:"driver"`
	Connstr        string `json:"connstr"`
	MaxOpenConns   int    `json:"max_open_conns"`
}

func (*DotDBProvider) Cleanup

func (dp *DotDBProvider) Cleanup(v any, err error) error

func (*DotDBProvider) MarshalJSON

func (d *DotDBProvider) MarshalJSON() ([]byte, error)

func (*DotDBProvider) MarshalText

func (d *DotDBProvider) MarshalText() ([]byte, error)

func (DotDBProvider) New

func (DotDBProvider) Type

func (DotDBProvider) Type() string

func (*DotDBProvider) UnmarshalJSON

func (d *DotDBProvider) UnmarshalJSON(b []byte) error

func (*DotDBProvider) UnmarshalText

func (d *DotDBProvider) UnmarshalText(b []byte) error

func (*DotDBProvider) Value

func (d *DotDBProvider) Value(r xtemplate.Request) (any, error)

type DotFS

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

DotFS is used to create an xtemplate dot field value that can access files in a local directory, or any fs.FS.

All public methods on DotFS are

func (*DotFS) Exists added in v0.5.1

func (c *DotFS) Exists(filename string) (bool, error)

Exists returns true if filename can be opened successfully.

func (*DotFS) List added in v0.5.1

func (c *DotFS) List(name string) ([]string, error)

List reads and returns a slice of names from the given directory relative to the FS root.

func (*DotFS) Open added in v0.5.1

func (c *DotFS) Open(path_ string) (fs.File, error)

Open opens the file

func (*DotFS) Read added in v0.5.1

func (c *DotFS) Read(filename string) (string, error)

Read returns the contents of a filename relative to the FS root as a string.

func (*DotFS) Stat added in v0.5.1

func (c *DotFS) Stat(filename string) (fs.FileInfo, error)

Stat returns Stat of a filename.

Note: if you intend to read the file, afterwards, calling .Open instead may be more efficient.

type DotFSProvider

type DotFSProvider struct {
	fs.FS `json:"-"`
	Path  string `json:"path"`
}

DotFSProvider can configure an xtemplate dot field to provide file system access to templates. You can configure xtemplate to use it three ways:

By setting a cli flag: “

func (*DotFSProvider) Cleanup added in v0.5.1

func (p *DotFSProvider) Cleanup(a any, err error) error

func (*DotFSProvider) MarshalJSON

func (d *DotFSProvider) MarshalJSON() ([]byte, error)

func (*DotFSProvider) MarshalText

func (fs *DotFSProvider) MarshalText() ([]byte, error)

func (DotFSProvider) New

func (DotFSProvider) Type

func (DotFSProvider) Type() string

func (*DotFSProvider) UnmarshalJSON

func (d *DotFSProvider) UnmarshalJSON(b []byte) error

func (*DotFSProvider) UnmarshalText

func (fs *DotFSProvider) UnmarshalText(b []byte) error

func (*DotFSProvider) Value

func (p *DotFSProvider) Value(r xtemplate.Request) (any, error)

type DotKV

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

func (DotKV) Value

func (d DotKV) Value(key string) string

type DotKVProvider

type DotKVProvider struct {
	Values map[string]string `json:"values"`
}

func (DotKVProvider) New

func (DotKVProvider) Type

func (DotKVProvider) Type() string

func (*DotKVProvider) Value

func (c *DotKVProvider) Value(xtemplate.Request) (any, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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