Skip to content

Template Helpers: Import and Embed Remote Templates Seamlessly. Plus other nice helpers.

License

Notifications You must be signed in to change notification settings

euforic/templit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

templit

Go Report Card GoDoc GitHub go.mod Go version Build Status GitHub

import "github.com/euforic/templit"

Variables

DefaultFuncMap is the default function map for templates.

var DefaultFuncMap = template.FuncMap{
    "lower":        strings.ToLower,
    "upper":        strings.ToUpper,
    "trim":         strings.TrimSpace,
    "split":        strings.Split,
    "join":         strings.Join,
    "replace":      strings.ReplaceAll,
    "contains":     strings.Contains,
    "hasPrefix":    strings.HasPrefix,
    "hasSuffix":    strings.HasSuffix,
    "trimPrefix":   strings.TrimPrefix,
    "trimSuffix":   strings.TrimSuffix,
    "trimSpace":    strings.TrimSpace,
    "trimLeft":     strings.TrimLeft,
    "trimRight":    strings.TrimRight,
    "count":        strings.Count,
    "repeat":       strings.Repeat,
    "equalFold":    strings.EqualFold,
    "splitN":       strings.SplitN,
    "splitAfter":   strings.SplitAfter,
    "splitAfterN":  strings.SplitAfterN,
    "fields":       strings.Fields,
    "toTitle":      strings.ToTitle,
    "toSnakeCase":  ToSnakeCase,
    "toCamelCase":  ToCamelCase,
    "toKebabCase":  ToKebabCase,
    "toPascalCase": ToPascalCase,
    "default":      defaultVal,
}

func ToCamelCase

func ToCamelCase(s string) string

ToCamelCase converts a string to CamelCase.

func ToKebabCase

func ToKebabCase(s string) string

ToKebabCase converts a string to kebab-case.

func ToPascalCase

func ToPascalCase(s string) string

ToPascalCase converts a string to PascalCase.

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase converts a string to snake_case.

type DefaultGitClient

DefaultGitClient provides a default implementation for the GitClient interface.

type DefaultGitClient struct {
    Token   string
    BaseURL string
}

func NewDefaultGitClient

func NewDefaultGitClient(defaultBranch, token string) *DefaultGitClient

NewDefaultGitClient creates a new DefaultGitClient with the optional defaultBranch and token.

func (*DefaultGitClient) Checkout

func (d *DefaultGitClient) Checkout(path, branch string) error

Checkout checks out a branch in a Git repository.

func (*DefaultGitClient) Clone

func (d *DefaultGitClient) Clone(host, owner, repo, dest string) error

Clone clones a Git repository to the given destination.

type DepInfo

DepInfo contains information about an embed URL.

type DepInfo struct {
    Host  string
    Owner string
    Repo  string
    Path  string
    Block string
    Tag   string
}

func ParseDepURL

func ParseDepURL(rawURL string) (*DepInfo, error)

ParseDepURL is a parsed embed URL.

type Executor

Executor is a wrapper around the template.Template type

type Executor struct {
    *template.Template
    git GitClient
}

func NewExecutor

func NewExecutor(client GitClient) *Executor

New returns a new Executor

func (*Executor) EmbedFunc

func (e *Executor) EmbedFunc() func(remotePath string, data interface{}) (string, error)

EmbedFunc returns a template function that can be used to process and embed a template from a remote git repository. EmbedFunc allows embedding content from a remote repository directly into a Go template.

Steps to use:

  1. Add the function to the FuncMap.
  2. Use the following syntax within your template:
    {{ embed "<host>/<owner>/<repo>/<path>@<tag_or_hash_or_branch>" . }}
    {{ embed "<host>/<owner>/<repo>#<block>@<tag_or_hash_or_branch>" . }}
    

Placeholders:

  • <host>: Repository hosting service (e.g., "github.com").
  • <owner>: Repository owner or organization.
  • <repo>: Repository name.
  • <path>: Path to the desired file or directory within the repository.
  • <block>: Specific template block name.
  • <tag_or_hash_or_branch>: Specific Git reference (tag, commit hash, or branch name).

func (*Executor) ImportFunc

func (e *Executor) ImportFunc(destPath string) func(repoAndTag, path string, data interface{}) (string, error)

ImportFunc returns a function that can be used as a template function to import and process a template from a remote git repository. ImportFunc allows embedding content from a remote repository into a Go template.

Steps to use:

  1. Add the function to the FuncMap.
  2. Use the following syntax within your template:
    `{{ import "<host>/<owner>/<repo>/<path>@<tag_or_hash_or_branch>" "<path_to_genrate_files>" . }}
    `{{ import "<host>/<owner>/<repo>/<path>@<tag_or_hash_or_branch>" "<path_to_genrate_files>" . }}
    

Placeholders:

  • <host>: Repository hosting service (e.g., "github.com").
  • <owner>: Repository owner or organization.
  • <repo>: Repository name.
  • <path>: Path to the desired file or directory within the repository.
  • <tag_or_hash_or_branch>: Specific Git reference (tag, commit hash, or branch name).

func (*Executor) ParsePath

func (e *Executor) ParsePath(inputPath string) error

ParsePath parses the given path

func (Executor) Render

func (e Executor) Render(name string, data interface{}) (string, error)

Render executes the template with the given data

func (Executor) StringRender

func (e Executor) StringRender(templateString string, data interface{}) (string, error)

StringRender renders the given template string with the given data

func (*Executor) WalkAndProcessDir

func (e *Executor) WalkAndProcessDir(inputDir, outputDir string, data interface{}) error

WalkAndProcessDir processes all files in a directory with the given data. If walkFunc is provided, it's called for each file and directory without writing the file to disk.

type GitClient

GitClient is an interface that abstracts Git operations.

type GitClient interface {
    Clone(host, owner, repo, dest string) error
    Checkout(path, branch string) error
    DefaultBranch() string
}

About

Template Helpers: Import and Embed Remote Templates Seamlessly. Plus other nice helpers.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages