github.com/coveo/gotemplate@v2.7.7+incompatible/docs/usage_long.md (about) 1 {% include navigation.html %} 2 3 {% raw %} 4 ## Examples 5 ```go 6 // Random text 7 {{ $text := lorem "paragraph" 5 10 }} 8 9 // Random email 10 {{ $email := lorem "email" }} 11 12 // Concatenation of various objects 13 {{ $string := concat "Hello" 1 " pi = " 3.14 " 3 x 5 = " (mul 3 5)}} 14 15 // Boolean conversion 16 {{ $bool := bool "True" }} 17 18 // Quote all elements of a list 19 {{ $quoted := list 1 2 3 | formatList "\"%v\"" }} 20 21 // Colored strings 22 {{ $colored := color "red" "I am red" }} 23 {{ $colored := color "red,bgwhite" "I am red with white background" }} 24 {{ $colored := color "red" "bgyellow" "I am %s with %s background" "red" "yellow" }} 25 26 // Define a script 27 {{ define "script" }} 28 #! /usr/bin/env python 29 30 for i in range({{ . | default 10 }}): 31 print("- ".format(i)) 32 {{ end }} 33 34 // Run the script (render the data as output by the python script) 35 {{ run "script" }} 36 37 // Run the script (returns a list of number from 0 to 24) 38 {{ $values := exec "script" 25 }} 39 40 // Get the content of a folder 41 {{ $values := run "ls" "-l" }} 42 43 // Get the content of the root folder (note that arguments could be either separated of embedded with the command) 44 {{ $values := run "ls -l /" }} 45 46 // Get the script in a variable and execute it later 47 {{ $script := include "script" 40 }} 48 {{ exec $script }} 49 50 // Add a function that could be called later (should be created in a .template file and used later as a function) 51 // Note that it is not possible to 52 {{ alias "generate_numbers" "exec" "script" }} 53 ... 54 // Can be later used as a function in another template script 55 {{ $numbers := generate_numbers 200 }} 56 {{ $numbers := 1000 | generate_numbers }} 57 ``` 58 59 ## Data structures 60 ```go 61 // JSON data definition 62 {{ $json_data := `"number": 10, "text": "Hello World!, "data": { "a": 1 }, "list": ["value 1, "value 2"]` | json }} 63 64 // YAML data definition 65 {{ $yaml_data := yaml ` 66 number: 10 67 text: Hello world! 68 data: 69 a: 1 70 b: 2 71 list: 72 - value 1 73 - value 2 74 ` }} 75 76 // HCL data definition from template 77 {{ define "source" }} 78 number = 10 79 text = "Hello world!" 80 81 data { 82 a = 1 83 b = 2 84 } 85 86 list = [ 87 "value 1", 88 "value 2", 89 ] 90 {{ end }} 91 92 {{ $hcl_data := hcl "source" }} 93 94 // YAML data definition for file 95 {{ $yaml_file := yaml "source.yml" }} 96 97 // Assign arbitrary data definition to a variable 98 // data can handle any HCL, JSON, YAML or string coming from the string directly or a template name or a file name 99 {{ $foo := `a = 1 b = 2 c = "Hello world!"` | data }} 100 ``` 101 {% endraw %}