github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs-src/content/functions/tmpl.yml (about)

     1  ns: tmpl
     2  title: template functions
     3  preamble: |
     4    Functions for defining or executing templates.
     5  funcs:
     6    - name: tmpl.Exec
     7      released: v3.3.0
     8      description: |
     9        Execute (render) the named template. This is equivalent to using the [`template`](https://golang.org/pkg/text/template/#hdr-Actions) action, except the result is returned as a string.
    10  
    11        This allows for post-processing of templates.
    12      pipeline: true
    13      arguments:
    14        - name: name
    15          required: true
    16          description: The template's name.
    17        - name: context
    18          required: false
    19          description: The context to use.
    20      examples:
    21        - |
    22          $ gomplate -i '{{define "T1"}}hello, world!{{end}}{{ tmpl.Exec "T1" | strings.ToUpper }}'
    23          HELLO, WORLD!
    24        - |
    25          $ gomplate -i '{{define "T1"}}hello, {{.}}{{end}}{{ tmpl.Exec "T1" "world!" | strings.Title }}'
    26          Hello, World!
    27    - name: tmpl.Inline
    28      alias: tpl
    29      released: v3.3.0
    30      description: |
    31        Render the given string as a template, just like a nested template.
    32  
    33        If the template is given a name (see `name` argument below), it can be re-used later with the `template` keyword.
    34  
    35        A context can be provided, otherwise the default gomplate context will be used.
    36      pipeline: false
    37      arguments:
    38        - name: name
    39          required: false
    40          description: The template's name.
    41        - name: in
    42          required: true
    43          description: The template to render, as a string
    44        - name: context
    45          required: false
    46          description: The context to use when rendering - this becomes `.` inside the template.
    47      examples:
    48        - |
    49          $ gomplate -i '{{ tmpl.Inline "{{print `hello world`}}" }}'
    50          hello world
    51        - |
    52          $ gomplate -i '
    53          {{ $tstring := "{{ print .value ` world` }}" }}
    54          {{ $context := dict "value" "hello" }}
    55          {{ tpl "T1" $tstring $context }}
    56          {{ template "T1" (dict "value" "goodbye") }}
    57          '
    58          hello world
    59          goodbye world
    60    - name: tmpl.Path
    61      released: v3.11.0
    62      description: |
    63        Output the path of the current template, if it came from a file. For
    64        inline templates, this will be an empty string.
    65  
    66        Note that if this function is called from a nested template, the path
    67        of the main template will be returned instead.
    68      pipeline: false
    69      rawExamples:
    70        - |
    71          _`subdir/input.tpl`:_
    72          ```
    73          this template is in {{ tmpl.Path }}
    74          ```
    75  
    76          ```console
    77          $ gomplate -f subdir/input.tpl
    78          this template is in subdir/input.tpl
    79          ```
    80    - name: tmpl.PathDir
    81      released: v3.11.0
    82      description: |
    83        Output the current template's directory. For inline templates, this will
    84        be an empty string.
    85  
    86        Note that if this function is called from a nested template, the path
    87        of the main template will be used instead.
    88      pipeline: false
    89      rawExamples:
    90        - |
    91          _`subdir/input.tpl`:_
    92          ```
    93          this template is in {{ tmpl.Dir }}
    94          ```
    95  
    96          ```console
    97          $ gomplate -f subdir/input.tpl
    98          this template is in subdir
    99          ```