I’m thinking of ways to implement a structured video object in the page head. Videos are inserted with a shortcode. Is there any way to get shortcode parameters, like video source and title, into a head partial? Or is putting it in the frontmatter the only option?
Trying to understand your problem. Does this sound right?
- User can insert video into content by using shortcode
- Shortcode params are at least: video source, title
- If this shortcode exists, you would like to run some logic in the head partial based off the shortcode params
Yes
Iterate over the shortcodes. So if two video shortcodes, then two structured objects in the head. Each with parameters from the respective shortcode.
Got it. Unfortunately, I don’t know of a way to solve it like that.
But, you could have a front matter param, that is an array of objects. Each object would have the video info. Then in your head, iterate this param and do your work as needed.
Yeah, that’s what I’m doing now. Just curious if there was another way. Thanks for your response
markdown
{{< video
src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4"
type="video/mp4"
width="640"
height="360"
description="This is Video A."
>}}
{{< video
src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_2MB.mp4"
type="video/mp4"
width="640"
height="360"
description="This is Video B."
>}}
layouts/shortcodes/video.html
{{ $id := printf "%04d" (add .Ordinal 1) }}
{{ $map := merge .Params (dict "id" $id) }}
{{ .Page.Scratch.SetInMap "videos" $id $map }}
<video id="{{ $id }}" width="{{ .Get `width` }}" height="{{ .Get `height` }}" controls>
<source src="{{ .Get `src` }}" type="{{ .Get `type` }}">
</video>
layouts/partials/video-stuff.html
{{ $noop := .Content }}
{{ $videos := .Scratch.GetSortedMapValues "videos" }}
{{ range $videos }}
{{ range $k, $v := . }}
{{ printf "%s = %s" $k $v }}<br>
{{ end }}<br>
{{ end }}
Sometimes there’s some concurrency weirdness if you update the templates while running hugo server
. So if you modify either the shortcode or the partial, restart hugo server
.
@jmooring Oh that’s cool. Didn’t know you could share Scratch context like that.
Am assuming the tool is smart enough to handle the render “re-ordering / constraining” behind the scenes. But have not looked for myself to confirm.
That’s the reason for the {{ $noop := .Content }}
statement before accessing the scratch. But like I said, there still appear to be some concurrency issues if you modify a template while running hugo server
, perhaps related to:
Ah, good to know, thx.
Ooh, this is cool. I’ll definitely try this out. Thanks
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.