V2 templates based on header user agent

1. My Caddy version (caddy version):

v2 RC3

3. The problem I’m having:

This didn’t really fit the help template so hope its okay if I removed those parts.

V1 Reference:

Long ago I used the suggestion in the link to create a bot/user template. I’m essentially doing this in a template:
{{ $uagent := .Header “User-Agent” }}
{{ $uagent := .Truncate $uagent 4 }}
{{if eq $uagent “xxxx” }}

<insert a template for BOT>

{{else}}

<insert a template for ELSE>

{{end}}

When i’m attempting to do this in V2, the Header isn’t available to parse for the user agent (at least that I could find) so this is failing now and I need another solution.

http.log.error template: templatecombined2:1:14: executing “templatecombined2” at <.Header>: can’t evaluate field Header in type fileserver.browseListing

I have two questions:

  1. Is there a V2 equivalent that I can use to get the header in the template to evaluate it?

  2. Is there a more efficient way to do this in V2 in the configuration itself by using matches?

i.e. something like this::
@template {
header xyz
}
use template xyz

and tell caddy if match to use one template else use the default? That would be far easier than my hacky way I did it previously.

Oops, I guess I forgot that field in the new docs.

You probably want something like this:

{{.Req.Header.Get "User-Agent"}}

Does that work for you?

Thanks Matt. I get an error with that field.

2020/04/23 13:06:11.797 ERROR http.log.error template: templatecombined2:1:18: executing “templatecombined2” at <.Req.Header.Get>: can’t evaluate field Req in type fileserver.browseListing

@matt I don’t see anything in tplcontext.go that handles this. I don’t think it was ported from v1.

Edit: Scratch that - templateContext has Req… should work…?

That should essentially be calling this function:

Along the same lines I get the same type of error when trying to use ‘Replace’ function as well. I’m not sure if it is different issue (Not trying to confuse this thread but figured it may not be worth creating a new one Separately so figured i’d mention )

ERROR	http.log.error	template: template:29:33: executing "template" at <$.Replace>: can't **evaluate field Replace in type fileserver.browseListing**

I worked around this using the config rather than sorting it out in the template. For anyone else who this might help. Simple enough.

@bottemplate {
   header User-Agent XXXX*
}
@regtemplate {
   not header User-Agent XXXX*
}
file_server @bottemplate {
    browse /data/caddy/bottemplate2
}
file_server @regtemplate {
    browse /data/caddy/regtemplate2
}

Ohhh you’re using templates for the file server’s browse feature, not the templates handler?? That wasn’t clear up front.

The browse template is executed in a totally separate context from that of the templates handler. I don’t have much documented on it right now. We can expand its functionality more in v2.x.

Exactly why you should always post your Caddyfile. Always.

2 Likes

Glad you got it working though Thanks for posting your temporary solution. It’s on my list to make the browse templates as functional as the other ones later.

1 Like

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