Helpers in Go templates: pipe and positional forms.

Browse this template in the repository →

The PDFs below are rendered live by PDF Server from the shipped examples.

markdown

Your browser can’t display this PDF inline.

Download the PDF — helpers_go_template / markdown.

helpers_go_template / markdown — Download PDF

timezone

Your browser can’t display this PDF inline.

Download the PDF — helpers_go_template / timezone.

helpers_go_template / timezone — Download PDF

utc

Your browser can’t display this PDF inline.

Download the PDF — helpers_go_template / utc.

helpers_go_template / utc — Download PDF

Inputs

Render data

markdown

examples/markdown.json

{
  "markdown_examples": {
    "multi_line": "line1\nline2",
    "list": "1. One\n2. Two"
  }
}

timezone

examples/timezone.json

{
  "time_examples": {
    "from_time":"2015-01-01T00:00:00+05:00",
    "to_time":"2015-01-01T00:00:00-05:00"
  }
}

utc

examples/utc.json

{
  "time_examples": {
    "from_time": "2022-04-01T12:34:56Z",
    "to_time": "2024-04-01T09:58:47Z"
  }
}

Data schema

schema.json

{
  "type": "object",
  "properties": {
    "time_examples": {
      "type": "object",
      "properties": {
        "from_time": {
          "type": "string",
          "format": "date-time"
        },
        "to_time": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "markdown_examples": {
      "type": "object",
      "properties": {
        "multi_line": {
          "type": "string"
        },
        "list": {
          "type": "string"
        }
      }
    }
  }
}

Template

template.tmpl

<html>
  <head>
  </head>
  <body>
    <section>
      {{ if .time_examples }}
        <h1>Format time</h1>
        <section>
          <h2>With pipe</h2>
          <p>
            From: {{ .time_examples.from_time | format_time "%Y-%m-%dT%H:%M:%S"  }} ({{ .time_examples.from_time }})
          </p>
          <p>
            To: {{ .time_examples.to_time | format_time "%A %d %B, %y" }} ({{ .time_examples.to_time }})
          </p>
          <p>
            Static: {{ "2014-01-01T00:00:00Z" | format_time "%A %d %B, %y" }}
          </p>
        </section>
        <section>
          <h2>Positional argument</h2>
          <p>
            From: {{ format_time "%Y-%m-%dT%H:%M:%S" .time_examples.from_time }} ({{ .time_examples.from_time }})
          </p>
          <p>
            To: {{ format_time "%A %d %B, %y" .time_examples.to_time }} ({{ .time_examples.to_time }})
          </p>
          <p>
            Static: {{ format_time "%A %d %B, %y" "2014-01-01T00:00:00Z" }}
          </p>
        </section>
      {{ end }}

      {{ if .markdown_examples }}
        <h1>Markdown</h1>

        <section>
          <h2>With pipe</h2>

          <h3>Multiline</h3>
          {{ .markdown_examples.multi_line | markdown }}

          <h3>List</h3>
          {{ .markdown_examples.list | markdown }}
        </section>
        <section>
          <h2>Positional argument</h2>

          <h3>Multiline</h3>
          {{ markdown .markdown_examples.multi_line }}

          <h3>List</h3>
          {{ markdown .markdown_examples.list }}
      {{ end }}
    </section>
  </body>
</html>