Using global variable in html issue

Probably a simple syntax thing but I can’t figure it out. To loop an embedded youtube video, I need to add &playlist=id. How do access $id again?

Current code (just scroll all the way to the right):

src="https://{{ $ytHost }}/embed/{{ $id }}{{ with .Get "autoplay" }}{{ if eq . "true" }}?autoplay=1&mute=1{{ end }}{{ end }}{{ with .Get "controls" }}{{ if eq . "false" }}&controls=0{{ end }}{{ end }}{{ with .Get "loop" }}{{ if eq . "true" }}&loop=1&playlist={{ $id }}{{ end }}{{ end }}" 

This yields the error:
Rebuild failed: “C:\Users\Kevin\Documents\kevinlinxc.github.io\content\video_editing\index.md:49:1”: failed to render shortcode “youtube”: failed to process shortcode: execute of template failed: html/template:shortcodes/youtube.html:12:274: {{$id}} appears in an ambiguous context within a URL

I don’t know how to fix this without refactoring. If I were writing this from scratch I would initialize vars for every item in the query string, pass a slice of key/val pairs through the querify function, and mark it as a safe URL value. Then append this to the protocol/host/path portion of the final src attribute.

For example…

{{- $s := slice "autoplay" $autoplay "controls" $controls "loop" $loop "mute" $mute }}
{{- with $playlist }}
  {{- $s = $s | append "playlist" . }}
{{- end }}
{{- $qs := querify $s | safeURL }}
{{- $src := printf "https://%s/embed/%s?%s" $ytHost $id $qs }}

<iframe {{ printf "src=%q" $src | safeHTMLAttr }}></iframe>