providers

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: Apache-2.0 Imports: 16 Imported by: 5

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

DotKV

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

DotDB

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

To use a driver, import its Go package while building your application.

DotFS

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 Dir added in v0.7.0

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

Dir

func (Dir) Dir added in v0.7.0

func (d Dir) Dir(name string) (Dir, error)

Dir returns a

func (Dir) Exists added in v0.7.0

func (d Dir) Exists(name string) bool

Exists returns true if filename can be opened successfully.

func (Dir) List added in v0.7.0

func (d Dir) List(name string) ([]fs.DirEntry, error)

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

func (Dir) Open added in v0.7.0

func (d Dir) Open(name string) (fs.File, error)

Open opens the file

func (Dir) Read added in v0.7.0

func (d Dir) Read(name string) (string, error)

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

func (Dir) Stat added in v0.7.0

func (d Dir) Stat(name 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 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 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