Clarification on template action .URL

In the docs on template actions, it says the following:

A part of the URL:

{{.URL.RawQuery}}

RawQuery returns the query string. You can replace RawQuery with Host, Scheme, Fragment, String, or Query.Get “parameter”.

I’d really like to be able to retrieve a particular query parameter, but all of the following make Caddy give a 500 Internal Server Error when I request a URL with the format mywebsite.com/page?parameter=something:

  • {{.URL.Query.Get "parameter"}}
  • {{.URL.Query.Get("parameter")}}
  • {{.URL.Query.Get["parameter"]}}
  • {{.URL.Query["parameter"]}}

When I print out the value for just {{.URL.Query}} I get map[parameter:[something]], but it’s not clear to me where to go from here.

I’m sure the root problem is I don’t know Go at all, but I can’t think of any other things to try. Could someone please provide some clarification on the syntax here?

Thanks!

After a whole lot more testing, I’ve found a solution. The following will display all of the query parameters and their values:

{{range $field, $val := .URL.Query}}
    {{$field}}
    {{range $woot := $val}}
        {{$woot}}
    {{end}}
{{end}}

It looks like you’ve asked one question and answered another.

This works for me (just tried it):

{{.URL.Query.Get "foo"}}

Not sure why you’re getting a 500. Maybe something else going on? You’ll have to look in the error log to be sure.

@matt Thanks for the response! With the information in your post, I was able to track down the real issue. My template is getting compiled to HTML before being used to render my Markdown files. During the compilation process, the quotes around the parameter name were getting converted to “smart” quotes. Explicitly telling the compiler to render them as regular double-quotes fixed everything. But before that, it was hard to tell what was wrong even staring at the compiled version because the quotes didn’t look that different…

Thanks again!

1 Like

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