Trying to use sprig function in template

1. Caddy version (caddy version):

2.3.0

2. How I run Caddy:

docker

a. System environment:

Docker

b. Command:

default

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

{
    order cgi last
}

(shared) {
  log

  encode gzip

  tls internal
}

dev.localhost {
  import shared

  cgi /*.py /usr/bin/python3 /usr/local/cgi/{path}

  file_server / {
    root /var/www
    browse /var/browse.html
  }
}
<table>
{{- range .Items}}
<tr class="file">
  <td>{{if hasSuffix .Name ".dev"}}YES{{end}}</td>
  <td>
    <a href="{{html .URL}}">
      {{- if .IsDir}}
      <svg width="1.5em" height="1em" version="1.1" viewBox="0 0 317 259"><use xlink:href="#folder{{if .IsSymlink}}-shortcut{{end}}"></use></svg>
      {{- else}}
      <svg width="1.5em" height="1em" version="1.1" viewBox="0 0 265 323"><use xlink:href="#file{{if .IsSymlink}}-shortcut{{end}}"></use></svg>
      {{- end}}
      <span class="name">{{html .Name}}</span>
    </a>
  </td>
  {{- if .IsDir}}
  <td data-order="-1">&mdash;</td>
  {{- else}}
  <td data-order="{{.Size}}">{{.HumanSize}}</td>
  {{- end}}
  <td class="hideable"><time datetime="{{.HumanModTime "2006-01-02T15:04:05Z"}}">{{.HumanModTime "01/02/2006 03:04:05 PM -07:00"}}</time></td>
  <td class="hideable"></td>
</tr>
{{- end}}
</table>

3. The problem I’m having:

In Caddy documentation it says “All Sprig functions are supported.”

The template is copied from Caddy default browse template.
Only changed the first td to try out hasSuffix from sprig.

I get an error on start.

4. Error messages and/or full log output:

run: loading initial config: loading new config: loading http app module: provision http: server srv0: setting up route handlers: route 0: loading handler 
modules: position 0: loading module 'subroute': provision http.handlers.subroute: setting up subroutes: route 1: loading handler modules: position 0: loading module 'file_server': provision http.handlers.file_server: parsing browse template file: template: browse.html:4: function "hasSuffix" not defined

5. What I already tried:

Not much. First time trying to use go template.

6. Links to relevant resources:

1 Like

Unfortunately, this is a known issue. The templates directive and the file server browser don’t share the same code for invoking templating, so many features are missing from browse.

https://github.com/caddyserver/caddy/issues/3637

1 Like

Ah OK. Thanks as always Francis! I’ll go watch that issue.

1 Like

You might want to remove the good first issue label on that issue…

The “good first issue” label is an invitation for people new to the repo to contribute. It’s not actually a difficult thing to solve, but someone needs to try it, and do it right.

Gotcha. Thanks for the explanation.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.