@codeshelldev/goplater
v1.5.1
Published
<p align="center"> <img width="256" height="256" alt="Goplater Logo" src="https://github.com/codeshelldev/goplater/raw/refs/heads/main/logo/goplater.png"> </p>
Readme
Contents
Getting Started
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.
Usage
Format
Goplater uses Go's builtin templating library therefor the syntax should be consistent with other projects.
Example:
File Content: +{{{ read "./myfile.txt" }}}File Functions
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…
read
Reads from absolute or relative file path (depending on input), where relative paths are relative to the invoker.
read "path"readArgs
Same as [read], but allows to supply additional arguments to the file for further processing.
read "path" arg1 arg2 arg3Arguments are accessible in under .args with {{{ index .args 0 }}}.
fetch
Performs a get http request to the specified url.
fetch "url"String Functions
trim
Outputs trimmed string.
upper
Outputs uppercased string.
lower
Outputs lowercased string.
contains
Returns wether string contains substring.
contains "Sunflower" "flower"count
Returns amount of times that substring is present in string.
repeat
Repeats string n times.
repeat "*" 5startsWith
Returns wether string starts with prefix.
startsWith "Sunflower" "Sun"endsWith
Returns wether string ends with prefix.
endsWith "Sunflower" "flower"isEmpty
Returns wether string is empty ("").
indexOf
Returns starting index of substring in string.
indexOf "Apple Banana Strawberry" "Apple"replace
Replaces substring with new string in old string.
replace "Sunflower" "flower" "shine"split
Split string by separator and return slice of all parts.
split "Apple, Banana, Strawberry" ", "before
Outputs string before substring in string.
before "Apple Banana Strawberry" "Banana"after
Outputs string after substring in string.
after "Apple Banana Strawberry" "Apple"between
Outputs inbetween of starting and ending substring.
between "Apple Banana Strawberry" "Apple" "Strawberry"slice
Slice string by start and end bounds.
slice " Apple " 2 7join
Joins strings with separator.
join "Apple" "Banana" "Strawberry" ", "concat
Concat multiple strings.
concat "Apple" "Banana" "Strawberry"append
Appends another string to string.
append "Hello " "World!"regexMatch
Outputs wether string matches regex.
regexMatch "[0-9]" "0123456789"regexFind
Returns a list of all regex matches.
index ( regexFind "[1-36-9]" "01234 56789" ) 0regexFindGroups
Returns a nested list ([][]string) of all regex submatches (groups (.*)).
index ( index ( regexFindGroups "(_*)(\S+)(_*)" "__xyz__" 0 ) 0 )regexReplace
Replaces substring via regex in string.
regexReplace "string" "replace_regex" "replace_with"Math Functions
add
Adds two numbers together.
sub
Subtraction for two numbers.
mult
Multiplies two numbers.
divd
Divides a through b.
mod
Performs a modulo b.
Conversion Functions
Functions for converting types.
toString
Returns value as string (via fmt.Sprint()).
toInt
Parses string as int.
toFloat64
Parses string as float64.
toFloat32
Same as toFloat64, but for float32.
toBool
Parses string as bool.
Container Functions
The following are functions for slices and maps.
has
Returns wether map or slice has key.
includes
Returns wether map or slice includes value.
delete
Deletes an entry from a map or slice.
delete map "key"delete slice 0set
Returns map or slice with updated key.
set map "key" valueset slice 0 valuemapCreate
Creates empty map (map[string]any).
mapCreateWith
Creates map by using arguments as key value pairs.
mapCreateWith "key1" value1 "key2" value2slicePush
Returns slice with value pushed on top.
slicePush slice valuesliceCreate
Creates empty slice.
sliceCreateWith
Creates slice by using arguments as items.
sliceCreate "a" "b" "c"Parser Functions
The following section is dedicated to parser functions, for example json.
jsonDecode
Parses json string as map.
jsonEncode
Returns json string from object.
yamlDecode
Parses yaml string as map.
yamlEncode
Returns yaml string from object.
base64Decode
Decodes base64 into raw string.
base64Encode
Encodes raw string into base64.
htmlDecode
Parses html string as html document.
htmlDocFind
Query element by selector in html document.
htmlDocFind ( html "html_string" ) "h3:contains['xyz']"htmlFind
Query element by selector within another element.
htmlFind ( htmlDocFind document ) "h3:contains['xyz']"htmlText
Outputs inner text of a html element.
htmlText ( htmlDocFind document "selector" )htmlAttr
Outputs the value of the specified elements attribute.
htmlAttr ( htmlDocFind document "selector" ) "attribute"htmlInner
Outputs element's inner html string.
htmlInner ( htmlDocFind document "selector" )Advanced Functions
In addition to the function in the Simple Functions section, there are also some functions for advanced usage.
import
Imports a file and executes it as template, output is discarded.
import "functions.inc.gtmpl"globalSet
Sets key globally to value.
globalSet "key" valueglobalGet
Returns global value at key.
funcDefine
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.
return
[!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.
returnNext
Same as return, but appends to output.
Shorthand for:
return i+1 valuereturnNext valuereturnAll
Sets return output slice directly with multiple arguments.
returnAll out1 out2 out3returnOutputs
Sets return output slice directly with one slice.
returnOutputs slicegetOutputs
Returns the whole output slice.
funcCall
Calls a global function by its name (without passing any arguments).
funcCall "name"Returns list of return outputs in order of index, or if applicable only a single output.
funcCallArgs
Same as funcCall, but arguments can be passed.
funcCallArgs "name" arg1 arg2Arguments are accessible in function body with {{{ index .args 0 }}}.
Contributing
Found a bug or just want to change or add something? Feel free to open up an issue or a PR!
Support
Like this Project? Or just want to help? Why not ⭐️ this Repo? :)
License
This Project is licensed under the MIT License.
Legal
Logo designed by @CodeShellDev — All Rights Reserved. Go gopher mascot originally created by Renée French, used under the CC BY 4.0 license.
