Delimit after 1 removes slice item?

This is a shortcode for scrambling and posting youtube playlists. Rather than messing with the youtube API, it takes ids from a json file, scrambles them and prints them to an iframe.

The only problem is it drops an id from the list at the delimit code, here

{{$ids = delimit (after 1 $ids) ", " }}

For example, 15 ids from the following json file, stored in data/playlists.json, go in, and only 14 are printed to the post. The post frontmatter has this variable: playlist = “Jazz 2”

{
"playlists": [
{
"name": "Jazz 2",
"description" : "I did the Samba to Cannonball Adderly",
"songs": "Cannonball Adderly: Autumn Leaves, Wabash, Toy, Groovy Samba, Oscar Peterson: Take the A train, The Girl from Ipanima, Sometimes Im Happy, Happy Go Lucky Localaka: Night Train,  A Little Jazz Exercise, Charlie Parker: Bloomdido, Dizzy Gillespie: One Note Samba, Con Alma, Beale Street, Flute Blues",
"ids": [
"IlxPpZC5OQY",
"e-S1Ftz4S-M",
"y7lJRnyRqMw",
"69uh2T1TtTY",
"_7WCzeylk08",
"WeDjbJMb1yw",
"8r1BAb9k7lg",
"_zTkpME1ATQ",
"UjNha6jgOOk",
"EF16My0SRo8",
"eUSYlOTP2_k",
"DggGN8E2vx0",
"J-3FwDJN9-s",
"he56MOJSC8A",
"YvwZ2kwfZ3s"
]
}
]
}

Here is the full shortcode:

{{$ids := slice "" }}
{{$list := "" }}
{{$description := "" }}
{{$songs := "" }}
{{if .Page.Params.playlist}}
{{$list = .Page.Params.playlist}}
{{else}}
{{$list = .Site.Params.playlist}}
{{end}}
{{$playlists := site.Data.playlists }}
{{range $playlists.playlists }}
{{if eq .name $list }}
{{range .ids}}
{{$ids = $ids | append . | shuffle }}
{{ end }}
{{$description = .name}}
{{$songs = .songs}}
{{ end }}
{{ end }}
{{$ids = delimit (after 1 $ids) ", " }}
<h5>Songs:</h5>
<p class= "p-news">{{- $songs -}}</p>
<div class="mobile-iframe">
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/VIDEO_ID?playlist={{- $ids -}};rel=0;modestbranding=1;iv_load_policy=3;showinfo=0;autohide=0;"frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>
</iframe>
</div> <!-- end mobile-iframe -->
<h5 class="center">Playlist Title:&nbsp;{{- $description -}}</h5>
<br/>

Can anyone see why $ids goes into this code with 15 ids and comes out with 14?

I think after is the culprit. What are you trying to accomplish with that line? :slight_smile:

1 Like

Something to do with musical chairs, lol :upside_down_face:

To print, the ids in the slice need to be comma delimited. after 1 works, but drops one id from the slice.

Without after 1, yet still delimited, all the ids are present and delimited, but the first id is followed by two commas.

Do you have something we can replicate?

{{ $x := slice "one" "two" "three" }}

{{ $y := delimit $x ", "}}
{{ $y }}   => one, two, three


{{ $z := delimit (after 1 $x) ", "}}
{{ $z }}   => two, three

after 1 is doing exactly what it is supposed to do, which is that it gives you a slice of the array after 1 item. So of course you will only have 14 given an initial 15-item array.

It would have been better to title this post ‘double comma after delimit.’ Without ‘after 1,’ the shortcode I posted above inserts two commas after the first id.

Can you replicate that?

The above snippet I posted is what I get when using delimit. No double commas. It would be easier to help diagnose if you had your code somewhere I can clone.

Yes.

You’re not iterating over 15 IDs. You’re iterating over an empty string plus 15 IDs. Change the first line of your shortcode from this:

{{$ids := slice "" }}

to this:

{{$ids := slice }}

Good eye Jmooring. That fixes it. Thanks. :+1:

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