# Need help with templatating

**URL:** <https://community.traefik.io/t/need-help-with-templatating/26593>\
**Category:** Traefik v3 (latest)\
**Tags:** file\
**Created:** [February 21, 2025, 9:23am UTC](https://community.traefik.io/t/need-help-with-templatating/26593 "2025-02-21T09:23:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WpqJ](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@WpqJ](https://community.traefik.io/u/WpqJ)\
**Post date:** [February 21, 2025, 9:23am UTC](https://community.traefik.io/t/need-help-with-templatating/26593/1 "2025-02-21T09:23:12Z")

</div>

I have several machines that expose a Syncthing endpoint, which I would like to route through Traefik. The target URL is of the form `http://{{ $host }}.funny-name.ts.net:8384`.

I wanted to template the configuration because all are identical, except for the hostname. I tried

```yaml
{{ define "routers" }}
        {{ $host := . }}
        syncthing-{{ $host }}:
            rule: Host(`{{ $host }}.my-domain.eu`)
            service: syncthing-{{ $host }}
{{ end }}

{{ define "services" }}
        {{ $host := . }}
        syncthing-{{ $host }}:
            loadBalancer:
                servers:
                    - url: http://{{ $host }}.funny-name.ts.net:8384
{{ end }}

http:
    routers:
{{ template "routers" "aaa" "bbb" }}
    services:
{{ template "services" "aaa" "bbb" }}

```

I was expecting the followig configuration to be generated:

```yaml
http:
    routers:
        syncthing-aaa:
            rule: Host(`aaa.my-domain.eu`)
            service: syncthing-aaa
       syncthing-bbb:
            rule: Host(`bbb.my-domain.eu`)
            service: syncthing-bbb
    services:
        syncthing-aaa:
            loadBalancer:
                servers:
                    - url: http://aaa.funny-name.ts.net:8384
         syncthing-bbb:
            loadBalancer:
                servers:
                    - url: http://bbb.funny-name.ts.net:8384

```

What I get instead is an error in the log (as soon as the file is updated)

```auto
traefik-1 | 2025-02-21T10:22:05+01:00 ERR Error occurred during watcher callback error="/config/config.d/testemplate.yaml: template: :18:22: executing \"\" at <\"aaa\">: can't give argument to non-function \"aaa\"" providerName=file
traefik-1 | 2025-02-21T10:22:05+01:00 ERR Error occurred during watcher callback error="/config/config.d/testemplate.yaml: template: :18:22: executing \"\" at <\"aaa\">: can't give argument to non-function \"aaa\"" providerName=file

```

What does this error mean and how to fix my template?

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [February 21, 2025, 11:46am UTC](https://community.traefik.io/t/need-help-with-templatating/26593/2 "2025-02-21T11:46:57Z")

</div>

According to [doc](https://doc.traefik.io/traefik/providers/file/#go-templating), Traefik uses go templating. Maybe you can find some validation tools and playgrounds for testing on the web.

---

<div class="post-metadata">

**Author:** ![WpqJ](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@WpqJ](https://community.traefik.io/u/WpqJ)\
**Post date:** [February 23, 2025, 12:47pm UTC](https://community.traefik.io/t/need-help-with-templatating/26593/3 "2025-02-23T12:47:07Z")

</div>

I misunderstood the way the `template` command works. Instead of

```auto
http:
    routers:
{{ template "routers" "aaa" "bbb" }}

```

I should have used

```auto
http:
    routers:
{{ template "routers" "aaa" }}
{{ template "routers" "bbb" }}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd81ebdb578656e76e56ff3cc3eed021d3ba132d.png) [@system](https://community.traefik.io/u/system)\
**Post date:** [February 26, 2025, 12:47pm UTC](https://community.traefik.io/t/need-help-with-templatating/26593/4 "2025-02-26T12:47:33Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
