Goplater is a Go commandline program that helps you template your files
Download the latest binary from the Release page.
Make it executable with chmod +x goplater and run it for the first time.
Use the goplater template command to template files:
./goplater template TEMPLATE.md -o README.mdThis will create a new file called README.md in your current working directory.
Goplater uses Go's builtin templating library therefor the syntax should be consistent with other projects.
Example:
File Content: ${{{ read "./myfile.txt" }}}
As you saw in the example above read is used for reading and output file contents.
But there are more as you will see in the following…
Reads from absolute or relative file path (depending on input), where relative paths are relative to the invoker.
read "path"
Same as read but with another parameter for additional arguments:
| Short | Long | Type | Note |
|---|---|---|---|
-r |
--recursive |
bool |
tries templating read files |
read "path" "--flag1" "--flag2"
Performs a get http request to the specified url.
fetch "url"
Outputs trimmed string.
Outputs uppercased string.
Outputs lowercased string.
Returns wether string contains substring.
contains "Sunflower" "flower"
Returns amount of times that substring is present in string.
Repeats string n times.
repeat "*" 5
Returns wether string starts with prefix.
startsWith "Sunflower" "Sun"
Returns wether string ends with prefix.
endsWith "Sunflower" "flower"
Returns wether string is empty ("").
Returns starting index of substring in string.
indexOf "Apple Banana Strawberry" "Apple"
Replaces substring with new string in old string.
replace "Sunflower" "flower" "shine"
Split string by separator and return slice of all parts.
split "Apple, Banana, Strawberry" ", "
Outputs string before substring in string.
before "Apple Banana Strawberry" "Banana"
Outputs string after substring in string.
after "Apple Banana Strawberry" "Apple"
Outputs inbetween of starting and ending substring.
between "Apple Banana Strawberry" "Apple" "Strawberry"
Slice string by start and end bounds.
slice " Apple " 2 7
Joins strings with separator.
join "Apple" "Banana" "Strawberry" ", "
Concat multiple strings.
concat "Apple" "Banana" "Strawberry"
Appends another string to string.
append "Hello " "World!"
Outputs wether string matches regex.
regexMatch "[0-9]" "0123456789"
Returns a list of all regex matches.
index ( regexFind "[1-36-9]" "01234 56789" ) 0
Returns a nested list ([][]string) of all regex submatches (groups (.*)).
index ( index ( regexFindGroups "(_*)(\S+)(_*)" "__xyz__" 0 ) 0 )
Replaces substring via regex in string.
regexReplace "string" "replace_regex" "replace_with"
Functions for converting types.
Returns value as string (via fmt.Sprint()).
Parses string as int.
Parses string as float64.
Same as toFloat64, but for float32.
Parses string as bool.
The following are functions for slices and maps.
Returns wether map or slice has key.
Deletes an entry from a map or slice.
delete map "key"
delete slice 0
Sets key in map or slice to value.
set map "key" value
set slice 0 value
Pushes value on top of slice.
slicePush slice value
Creates slice by using arguments as items.
sliceCreate "a" "b" "c"
The following section is dedicated to parser functions, for example json.
Parses json string as map.
Returns json string from object.
Parses yaml string as map.
Returns yaml string from object.
Decodes base64 into raw string.
Encodes raw string into base64.
Parses html string as html document.
Query element by selector in html document.
htmlDocFind ( html "html_string" ) "h3:contains['xyz']"
Query element by selector within another element.
htmlFind ( htmlDocFind document ) "h3:contains['xyz']"
Outputs inner text of a html element.
htmlText ( htmlDocFind document "selector" )
Outputs the value of the specified elements attribute.
htmlAttr ( htmlDocFind document "selector" ) "attribute"
Outputs element's inner html string.
htmlInner ( htmlDocFind document "selector" )
In addition to the function in the Simple Functions section, there are also some functions for advanced usage.
Imports a file and executes it as template, output is discarded.
import "functions.inc.gtmpl"
Sets key globally to value.
globalSet "key" value
Returns global value at key.
Defines a global function.
funcDefine "name" `
{{{ return 0 "Hello World" }}}
`
The second argument is the template body, notice the {{{ ... }}} instead of ${{{ ... }}}.
Raw output is discarded only output via return persists.
Warning
This function is only accessible from within functions!
Sets return argument at index to value.
funcDefine "helloWorld" "{{{ return 0 "Hello World!" }}}"
Overwriting previous return arguments is possible.
Same as return, but appends to output.
Shorthand for:
return i+1 value
returnNext value
Sets return output slice directly with multiple arguments.
returnAll out1 out2 out3
Sets return output slice directly with one slice.
returnOutputs slice
Returns the whole output slice.
Calls a global function by its name (without passing any arguments).
funcCall "name"
Returns list of return outputs in order of index.
Same as funcCall, but arguments can be passed.
funcCallArgs "name" arg1 arg2
Arguments are accessible in function body with {{{ index .args 0 }}}.
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.
