Originally created by R. Martinelli, currently maintained by DO3B.
A Go-based template management tool greatly inspired by Kustomize remote resources management.
XLTemplate is a Go-based templating engine designed for managing and processing templates with a hierarchical structure. It draws inspiration from Kustomize's approach to managing Kubernetes configurations, allowing for flexible template organization and composition. XLTemplate supports loading templates from Git repositories and local file systems, making it adaptable to various development workflows.
- Flexible Template Sourcing: Load main templates and patterns from Git repositories (with branch/tag support) or the local file system.
- Hierarchical Structure: Organize templates in a nested manner, promoting reusability and modularity.
- YAML Configuration: Define template processing behavior through a clear and concise
xltemplate.yamlfile. - Pattern/Library System: Include reusable template snippets (patterns or libraries) from various sources.
- Variable Injection: Parameterize templates using external YAML files for variables, with support for merging multiple variable sources.
- Go Template Syntax: Utilizes standard Go template syntax, offering powerful built-in functions and logic.
- Sprig Function Library: Enhanced templating capabilities with the inclusion of the Sprig library of template functions.
xltemplate operates on a few key concepts to provide its flexibility and power:
The primary template file that xltemplate processes is defined by the source field in the xltemplate.yaml configuration file. xltemplate supports two main types of sources:
- Git Repositories: You can specify a template file directly from a Git repository by providing a URL. The URL format allows you to specify a path to a file within the repository, and optionally a specific branch, tag, or commit hash using the
?ref=query parameter. Example:https://github.com/user/repo///path/to/template.tmpl?ref=main - Local File System: Templates can also be loaded from your local file system. Simply provide a relative or absolute path to the template file.
Example:
path/to/your/template.tmplor/abs/path/to/template.tmpl
Variables allow you to customize your templates by injecting dynamic data. They are defined in a separate YAML file, which is specified by the variables field in the xltemplate.yaml configuration.
- YAML Format: Variables are defined using standard YAML syntax. You can create simple key-value pairs, lists, and nested objects.
Example (
variables.yaml):projectName: "My Awesome Project" version: "1.2.3" features: - name: "Feature A" enabled: true - name: "Feature B" enabled: false
- Usage in Templates: Variables are accessed in your Go templates using dot notation (e.g.,
{{ .projectName }},{{ .version }},{{ range .features }}{{ .name }}{{ end }}). - Includes: The variables file can also contain a special
:includes:key. This key takes a list of other YAML file paths that will be merged into the main variables structure. This allows for better organization and reuse of common variable definitions. Files listed later in the:includes:list will override values from earlier ones if keys conflict.
Patterns provide a way to include and reuse other templates, often referred to as "libraries" or "partials". These are specified using the patterns field in xltemplate.yaml, which takes a list of sources.
- Source Types: Similar to the main
source, patterns can be loaded from Git repositories or the local file system. Each entry in thepatternslist should point to a directory. Example (xltemplate.yaml):patterns: - "path/to/local/library_directory/" - "https://github.com/user/repo///path/to/libs/?ref=main"
- Structure: All template files (typically
.tmplor.tplfiles) within the specified pattern directories become available for inclusion. - Usage: You can include these library templates in your main template (or other library templates) using the
{{ include "templateName" . }}directive. ThetemplateNamecorresponds to the filename of the library template (without the extension). For instance, a file named_header.tmplin a pattern directory would be included as{{ include "_header" . }}. You can pass data (context) to the included template.
The final rendered content needs to be saved, and this is defined by the output field in the xltemplate.yaml configuration file.
- File Path: You specify the desired path and filename for the generated output. If the file doesn't exist,
xltemplatewill create it. If it does exist, it will be overwritten. Example (xltemplate.yaml):output: "dist/rendered_config.yaml"
- Standard Output: If the
outputfield is omitted or left empty,xltemplatewill print the rendered content to the standard output (stdout). This is useful for piping the output to other tools or for quick inspection.
These core concepts work together to allow xltemplate to fetch, process, and render templates in a structured and manageable way.
You can install XLTemplate in the following ways:
Ensure you have Go installed on your system.
# To install the binary to your GOPATH/bin
go install do3b/xltemplate@latest
# Alternatively, to build the binary in the current directory
go build -v -o xltemplate do3b/xltemplateMake sure your GOPATH/bin is in your system's PATH environment variable if you use go install.
If you have Task installed, you can build the project using the provided Taskfile.yml:
task buildThis typically places the compiled xltemplate binary in the project's root directory.
This section explains how to use xltemplate with the provided sample/ directory as an example.
The xltemplate.yaml file defines the source template, variables, patterns (libraries), and output file.
source: https://github.com/DO3B/xltemplate///cmd/sample/demo.tmpl?ref=feature/tentative-gin
variables: variables.yaml
patterns:
- https://github.com/DO3B/xltemplate///sample/lib/
- lib/
output: template.yaml- source: Specifies the main template file. It can be a local path or a URL.
- variables: Points to a YAML file containing variables to be injected into the templates.
- patterns: A list of paths or URLs to directories containing library templates. These libraries can be included in other templates.
- output: The name of the file where the final rendered template will be saved.
This is the main template file referenced by the source field in xltemplate.yaml.
Template inclusion compatible with sprig functions
{{include "library" .books}}
Trigger {{.missing}} warning- It uses Go template syntax.
{{include "library" .books}}includes a library template named "library" (defined insample/lib/library.tmpl) and passes the.booksvariable to it.{{.missing}}demonstrates a variable that might not be defined, which would result in a warning during processing.
This file defines the variables that will be available in the templates.
:includes:
- common.yaml
version: 1.0.0
books:
- title: Birth
collection: My Life
authors:
- A. Mitchel
- F. Lu
- title: Io ucciso
authors:
- G. Faletti- :includes: (Optional) A special key to include other YAML files. In this case, it attempts to include
common.yaml(which might not exist in this minimal example, illustrating the feature). - version: A simple string variable.
- books: A list of book objects, each with a
title, optionalcollection, and a list ofauthors.
This file defines a reusable template library. Files in directories specified under patterns in xltemplate.yaml are treated as library templates.
{{define "library"}}
{{- range .}}
{{.title}}{{with .collection}} (Part of {{.}} collection){{end}}
{{ include "authors" . }}
{{end -}}
{{end}}{{define "library"}}...{{end}}defines a template block named "library". This name is used in theincludefunction insample/demo.tmpl.- It ranges over a list of items (expected to be the
booksvariable). - For each item, it displays the
titleandcollection(if present). - It also includes another template named "authors". Note: The "authors" template is not defined in this sample, which would typically be part of the library to render author information. For this example, we'll assume its absence or it could be added to
library.tmplor another file inlib/.
To generate the output based on the sample configuration, navigate to the sample directory and run the following command:
go run ../xltemplate/main.go build xltemplate.yamlThis command tells xltemplate to:
- Use the
buildsubcommand. - Load its configuration from the
xltemplate.yamlfile in the current directory (sample/).
After running the command, xltemplate will generate a file named template.yaml (as specified in output in sample/xltemplate.yaml) in the sample directory with the following content:
Template inclusion compatible with sprig functions
Birth (Part of My Life collection)
Written by A. Mitchel,F. Lu
Io ucciso
Written by G. Faletti
Trigger <no value> warningThis output is the result of processing sample/demo.tmpl with the variables from sample/variables.yaml and the library templates from sample/lib/. The {{.missing}} variable in demo.tmpl resulted in the "Trigger warning" line. The authors are not fully rendered as the authors template was not defined.
Contributions are welcome! Please feel free to submit a Pull Request.