Skip to content

Using Built-in Helpers

The template rendering commands include a set of built-in helper functions to assist you with the rendering process.

Functions

include

 func include(template string, data any) string

This function allows you to invoke your own custom helper functions.

e.g.

{{ include "sayHello" $data }}

This function can also be used to render a file.

e.g.

{{ include "./path/to/file.yaml" . }}

The path to template file to render is relative to the parent template file.

os_writefile

func os_writefile(dest string, content string) string

Write a file to the output directory

The destination path is relative to the output directory. ( ".." or absolute paths are not allowed)

This function outputs the destination file path.

e.g.

{{ os_writefile "./dst/filename.txt" "contents" }}

os_copyfile

func os_copyfile(dest string, src string) string

Copies files to the output directory. This function outputs the destination file path as a string.

The destination path is relative to the output directory ( ".." or absolute paths are not allowed)

The source path is relative to the main template file directory ( ".." or absolute paths are not allowed)

e.g.

{{ os_copyfile "./dest/lib.jar" "./src/lib.jar" }}

os_getenvs

func os_getenvs() map[string]string

Gets all environment variables as a dictionary

e.g.

{{ $envs := os_getenvs }}

os_getenv

func os_getenv**(env string) string

Gets the value of the specified env var

e.g.

{{ os_getenv "USER" }}

slug_make

func slug_make(in string) string

Converts string to a slug

e.g.

{{ slug_maek "My API proxy" }}
The example above outputs "my-api-proxy"

url_parse

func url_parse(url string) net.URL

Parse a URL into its parts.

This function outputs a net.URL struct.

e.g.

{{ $url := url_parse "https://example.com/foo/bar" }}

blank

func blank() string

Outputs empty string. This is useful to consume the output of another function.

e.g.

{{ os_writefile "./dest/file" "foo" | blank }}

deref

func deref(*any) any

Dereferences the input pointer.

fmt_printf

func fmt_printf(pattern string, args ... string)

Write to stdout during the rendering process. This function is useful for so called "printf" debugging.

For example, you can use it to trace the template rendering as it runs. Or, can also use it to dump values to stdout in order to see the contents.

e.g.

{{ fmt_printf "Hello World\n" }}

{{ fmt_printf "url: %%v\n" $url }}

oas3_to_mcp

func oas3_to_mcp**(file string) map[string]any

Extracts MCP metadata from an OpenAPI 3.x description

The result contains tools_list and tools_targets sections.

  • The tools_list is an array that can be used for MCP tools/list response.
  • The tools_targets is a map with information useful for transcoding MCP tool/calls to REST.

e.g.

{{ $mcpValues := oas3_to_mcp "./path/to/openapi.yaml" }}
The file path is relative to the main template file directory.

{
  // A list of tool definitions.
  tools_list: [
    {
      name: "...",                 // The unique operationId
      title: "...",                // The operation summary.
      description: "...",          // The operation description.
      inputSchema: { ... },        // JSON Schema with all query, header, path and the request body
      outputSchema: { ... },       // JSON Schema for the successful response body.
    },
    ...
  ],

  // A map of tool targets
  tools_targets: {
    [tool_name]: {                 // The unique operationId
      verb: "...",                 // The target API HTTP method (e.g., "GET", "POST").
      pathSuffix: "...",           // The target API path template (e.g., "/users/{userId}").
      contentType: "...",          // The Content-Type header to sent to target
      accept: "...",               // The Accept header to send to target
      pathParams: ["...", ... ],   // List of target path parameter names.
      queryParams ["...", ... ],   // List of target query parameter names.
      headerParams: ["...", ... ], // List of target header parameter names.
      payloadParam: "...",         // Name of the payload parameter used for the request body.
      payloadSchema: { ... },      // JSON Schema for the target request body
      responseSchema: { ... }      // JSON Schema for the target response body
    }
  }
}

Libraries

Sprig

This library contains a lot of useful functions for string manipulation, accessing maps, lists, encoding, and more.

Functions from Sprig library are available during rendering.

e.g.

{{ "Hello World" | upper }}

{{ list "hello" "world" | join "_" }}