Shortcode failing with nested {{ if }}

I’m trying to write a shortcode to present bitchute videos. The shortcode was read Ok (after a few glitches where the layouts/partial/bitchute.html was rejected, but as soon as I try to apply it, I get

failed to render shortcode "bitchute": failed to process shortcode: execute of template failed: html/template:shortcodes/bitchute.html:11:37: {{if}} branches end in different contexts: {stateAttr delimSpaceOrTagEnd urlPartNone jsCtxRegexp attrNone elementNone <nil>}, {stateTag delimNone urlPartNone jsCtxRegexp attrNone elementNone <nil>}

I have cut the shortcode back to try to isolate the problem, and the above error is occurring with this version of the shortcode.

{{ $class := .Get "class" }}
{{ $title := .Get "title" }}
{{ $autoplay := .Get "autoplay" }}
{{ $id := .Get "id" }}
{{ $url := .Get "url" }}
<div {{ with $class }}class="{{ . }}"{{ else }}style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;"{{ end }}>
    {{ printf "%#v, " (slice $title $class $autoplay $id $url) }}
    {{ if $id }}
        {{$embedurl := (printf "https://www.bitchute.com/embed/%s/" $id) }}
        {{ printf "%#v, " (slice $embedurl $title $class $autoplay $id $url) }}
        <iframe src="$embedurl"{{ if eq $autoplay "true" }}?autoplay=1{{ end }}></iframe>

{{/*
        <iframe src="$embedurl"{{ if eq $autoplay "true" }}?autoplay=1{{ end }}
            {{- if not $class }} style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" {{ end -}}
            allowfullscreen title="{{ $title }}"></iframe>
    {{ else if $url }}
        <iframe src="{{ replace $url `/video/` `/embed/` }}{{ if eq $autoplay `true` }}?autoplay=1{{ end }}"
        {{- if not $class }} style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" {{ end }}allowfullscreen title="{{ $title }}"></iframe>
*/}}    
    {{ end }}
</div>

As soon as the “if” in {{ if eq $autoplay “true” }} is read, it barfs. What am I doing wrong?

The error tells you, that your quotation mark " is at the wrong spot. Put it behind the if-part. Go or Hugo, one of them is extremely clever and knows that you are adding outside of the HTML-attribute. Proper way is this:

<iframe src="$embedurl{{ if eq $autoplay "true" }}?autoplay=1{{ end }}"></iframe>

Thanks for that. It seems that I also need to expand the $embedurl using

<iframe src="{{$embedurl}}{{ if eq $autoplay "true" }}?autoplay=1{{ end }}"></iframe>```

Otherwise I just get the string '$embedurl' in the src attribute.

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