TEMPLATE.md
goplater template TEMPLATE.md -o README.mdREADME.md
# MY-APP
Version: 1.2.3
## Install
Run `npm install my-app`This very README is generated with Goplater.
Need help? Come join our Matrix Server!
- Familiar. It is Go's
text/templatesyntax. If you know it, you already know Goplater. - Batteries included. Read and write files, fetch URLs, work with JSON, YAML, HTML and Markdown, run regex and string operations. See the Standard Library.
- Write your own functions, inside the template. Define them with
define, call them withcall. Arguments and return values included. - Share and import functions. Put your functions in a file, import them from disk or from a URL and reuse them everywhere.
- Not just a CLI. The engine behind it,
gplt-engine, is a separate library you can embed in your own Go program. - Modular. The library is split into independent modules. Load only what you need. Or let the CLI load them all.
- Automated. Use the GitHub Action and Bot to render templates and automate pull requests. See Automation.
Install with Go:
go install github.com/codeshelldev/goplater@latestOr download the latest binary from the Releases page, make it executable with chmod +x goplater and run it.
Template your first file:
goplater template examples/app-readme/template.md -o README.mdThis creates README.md in your current working directory.
Goplater uses Go's built-in templating library, with actions written as +{{ ... }}. Files that already contain plain {{ }} (Helm charts, GitHub Actions, Mustache) pass through untouched.
Point Goplater at a directory and it mirrors the structure into the output:
goplater template ./examples -o ./out -rUseful flags:
| Flag | What it does |
|---|---|
-o |
Output file or directory |
-s |
Source directory for ~/ paths |
-r |
Walk directories |
--match |
Only template files matching a regex |
--flatten |
Drop the directory structure |
--root |
Choose the root that output paths are relative to |
--allowed-outputs |
Limit where templates may write |
--trim-max-blank-lines |
Experimental: Remove blank lines left behind by non-output actions |
--dry-run |
Show what would be written, change nothing |
--ignore-errors |
Continue processing when errors occur |
Define a function in the template and call it:
Output:
Prefer Go? Register custom Go functions as well.
Keep your functions in one file and import them from the filesystem or from a URL:
Pin remote imports to a tag or a commit and treat them like any other dependency: imported functions run inside your template.
gplt-engine is not tied to the CLI.
Load the modules you want and embed templating in your own Go application.
Browse the packages on pkg.go.dev.
Use Goplater in your CI/CD workflows to generate files automatically.
- Goplater GitHub Action: render templates directly in your GitHub Actions workflows.
- Goplater Bot: automate file generation in a repository and have the results submitted as a pull request or pushed as a commit.
We use Goplater Bot as well, check him out PR #20!
gplt-engine includes a large and growing selection of built-in functions for use in your templates.
Every module is independent, load all of them or only the ones you need.
Goplater ships with all implemented modules in addition to fs-specific and cli dependent functions.
| Module | Use it for |
|---|---|
fs |
Reading, writing and walking files and directories |
outputto |
Choosing the output path from inside a template |
strings, regex |
Trimming, splitting, replacing, matching |
json, yaml |
Decoding and encoding structured data |
html, markdown |
Querying and editing documents |
http |
Fetching remote resources |
math |
Operators and number helpers |
container |
Maps and slices |
base64, conversion |
Encoding and type conversion |
import, return, globals |
Sharing functions and passing values around |
debug |
Printing while you develop |
Need something that is not there? Add your own with a custom module or your own Goplater function file and share it.
|
Reads and templates file. ExamplesExample: |
|
Reads file (without templating). ExamplesExample: |
|
Reads file and passes arguments to it.
Arguments are available as ExamplesExample: |
|
Writes to a file path. ExamplesExample: |
|
Creates directory at path. ExamplesExample: |
|
Appends to an existing file. ExamplesExample: |
|
Returns if a file or folder exists at a given path. ExamplesExample: |
|
Returns if a file exists at a given path. ExamplesExample: |
|
Returns if a folder exists at a given path. ExamplesExample: |
|
Returns all files and folders inside of a given directory. ExamplesExample: Output: |
|
Returns all files and folders recursively under a given directory. ExamplesExample: Output: |
|
Removes a file or folder at a given path. ExamplesExample: |
|
Joins paths together. |
|
Returns the last element of a path. |
|
Returns all but the last element of a path. |
|
Returns the file name extension of a file path. |
|
Returns an absolute representation of a given path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. |
|
Returns a relative path that is lexically equivalent to targetPath when joined to basePath with an intervening separator. The returned path will always be relative to basePath, even if basePath and targetPath share no elements. |
|
Changes the output path for the current file. Errors if path is not allowed. |
|
Defines a global value with a key. |
|
Tries to retrieve a global value with a key. |
|
Returns whether a given global variable exists. |
|
Tries to import a template file with func definitions.
Optionally supply an alias for easier access.
The fs resolver is only available as an internal module and does not ship with the ExamplesExample: Output: Example: Output: |
|
Same ExamplesExample: Output: |
|
Sets an output value at the given index. |
|
Appends a value to the output. |
|
Sets the output object as a whole. ExamplesExample: |
|
Returns current outputs. |
|
Decodes html into a document. |
|
Finds elements in document by selector (using goquery.Find). |
|
Finds element in document by selector (using goquery.Find). |
|
Finds elements in element by selector (using goquery.Find). |
|
Finds element in element by selector (using goquery.Find). |
|
Returns the elements text content. |
|
Returns an elements attribute content. |
|
Returns the elements inner html. |
|
Decodes markdown into a document. |
|
Encodes a document into markdown. |
|
Finds elements in target by selector (using custom). |
|
Finds element in target by selector (using custom). |
|
Returns whether node matches selector (using custom). |
|
Returns html markdown representation. |
|
Returns all raw markdown text in a node recursively. |
|
Returns all markdown headings. |
|
Returns all markdown links. |
|
Returns the url of a markdown link node. |
|
Sets a link node's url. |
|
Sets a link node's title. |
|
Returns the text of a markdown link node. |
|
Returns all markdown images. |
|
Returns the url of a markdown image node. |
|
Sets a image node's url. |
|
Sets a image node's alt. |
|
Returns all markdown paragraphs. |
|
Returns all markdown code blocks. |
|
Returns all markdown block quotes. |
|
Removes a markdown node. |
|
Appends text to a markdown document. |
|
Prepends text to a markdown document. |
|
Encodes an object into a yaml string. |
|
Decodes a yaml string into an object. |
|
Deletes a container entry with the given key. ExamplesExample: Output: |
|
Set a container entry value with the given key. ExamplesExample: Output: |
|
Returns if an entry with the given key exists in container. ExamplesExample: |
|
Appends a value to a slice. ExamplesExample: Output: |
|
Creates a new slice, optionally with values. ExamplesExample: Output: |
|
Creates a new map, optionally with key value pairs. Pairs are constructed one by one, a key followed by the given value. Currently only supports string keys! ExamplesExample: Output: |
|
Returns a given value as string (using fmt.Sprint, bytes are handled separately). |
|
Turns a string into bytes. |
|
Attempts to parse a string as int. |
|
Attempts to parse a string as float64. |
|
Attempts to parse a string as float32. |
|
Attempts to parse a string as a bool (using strconv.ParseBool). |
|
Outputs data to stdout. |
|
Encodes strings with base64. |
|
Decodes a base64 string. |
|
Encodes a string as hex. |
|
Decodes a string as hex. |
|
Hashes a given string with SHA-256. |
|
Hashes a given string with SHA-1. |
|
Hashes a given string with SHA-512. |
|
Hashes a given string with MD-5. |
|
Fetches a resource from a given url. Outputs errors as string. |
|
Encodes an object into a json string. |
|
Decodes a json string into an object. |
|
Returns the abs value of a number. |
|
Returns the smaller value of two numbers. |
|
Returns the bigger value of two numbers. |
|
Clamps a value between a minimum and maximum value. |
|
Performs pow on base with exponent. |
|
Returns the square root of a number. |
|
Rounds a number to the nearest integer. |
|
Returns the greatest integer value less than or equal to number. |
|
Returns the least integer value greater than or equal to number. |
|
Adds numbers together. |
|
Subtracts numbers from each other. |
|
Multiplies numbers together. |
|
Divides numbers from each other. |
|
Performs modulo on numbers. |
|
Returns whether a string matches the given regex pattern. |
|
Returns all matches against the given regex pattern in a string. |
|
Returns all matches and submatches from the given regex pattern in a string. |
|
Returns all match and submatch positions from the given regex pattern in a string. |
|
Replaces all matches from the given regex pattern in a string with the replaceWith param (which allows using '$1', etc.). |
|
Trims space from a string (leading and trailing). |
|
Trims all chars defined in the cutset param from a string (leading and trailing). |
|
Uppercases a string. |
|
Lowercases a string. |
|
Returns whether a string contains the given substring. |
|
Counts all occurences of a substring in the given string. |
|
Returns whether a string starts with the given prefix. |
|
Returns whether a string ends with the given suffix. |
|
Returns whether the given string is empty. |
|
Replaces all occurences of a substring in the given string with the replaceWith param. |
|
Split a string by the given seperator. |
|
Returns the string after the given substring in a string. |
|
Returns the string after the last occurrence of the given substring. |
|
Returns the string before the given substring in a string. |
|
Returns the string before the last occurrence of the given substring. |
|
Returns the substring between the start and end substring. |
|
Removes a substring at the start of the given string. |
|
Removes a substring at the end of the given string. |
|
Slices a string based on start and end index. |
|
Joins multiple strings together by the given separator. |
|
Repeat a string n times. |
Found a bug or just want to change or add something? Feel free to open up an issue or a PR!
Like this Project? Or just want to help? Why not ⭐️ this Repo? :)
This Project is licensed under the MIT License.
Logo designed by @CodeShellDev — All Rights Reserved.
Go gopher mascot originally created by Renée French, used under the CC BY 4.0 license.
