Syntax

Learn about the syntax of templates.

Templates syntax

  • Accessing keys

    You can access the keys in the JSON object by using the {{ .key }} syntax. The key is case sensitive and should be used as it is in the JSON object.

    • Example

      // data source
      {
          "key": "Customer"
      }
      
      // nested key
      {
          "key": {
              "nestedKey": "Nested"
          }
      }
      
      // template
      {{ .key }} Form
      
      // nested key
      {{ .key.nestedKey }}
      
      // generated code
      Customer Form
      Nested
  • Looping through keys

    You can loop through by using the {{ range .key }} syntax. The range keyword is used to loop through the keys in the JSON object. The . is used to access the current key in the loop.

    • Example

      // data source
      {
          "country": "India",
          "cities": [
              {
                  "name": "Mumbai"
              },
              {
                  "name": "Delhi"
              }
          ]
      }
      // template
      {{ range .cities }} {{ .name }}, {{ end }}
      
      // Accessing parent context inside loop
      {{ range .cities }} {{ .name }} is in {{ $.country }}{{ end }}
      
      // generated code
      Mumbai, Delhi
      
      // Accessing parent context inside loop
      Mumbai is in India
      Delhi is in India
  • If statements

    You can use if statements using if and else keywords.

    • Example

      // data source
      {
          "key": "Customer",
          "isActive": true
      }
      
      // template
      User is {{ if .isActive }}Active{{ else }}Inactive{{ end }}
      
      // generated code
      User is Active
  • Functions

    You can use functions in the templating syntax to manipulate the data in runtime.

    • Example

      // data source
      {
          "key": "Customer"
      }
      
      // template
      {{snakeCase .key }}
      {{camelCase .key }}
      {{kebabCase .key }}
      {{flatCase .key }}
      
      // generated code
      customer_form
      customerForm
      customer-form
      customerform

On this page