OpenGraph with custom image parameter

I want to add OpenGraph metadata to my pages using the internal OpenGraph template. However, that expects images to be referenced in .Page.images in the front matter. However, for my theme I’m using .Page.bannerimage in the front matter to set a featured image. How can I use the OpenGraph template, without having to stick to .Page.images in every article?

I tried calling the OpenGraph template with

{{ template "_internal/opengraph.html" (merge . (dict "Params" (dict "images" (slice .Params.images .Params.bannerimage.src)))) }}

But this fails with

execute of template failed at <merge . (dict "Params" (dict "images" (slice .Params.images .Params.bannerimage.src)))>: error calling merge: source must be a map, got page.Page

so apparently you can’t merge additional parameters into the page parameters? What am I missing? Thanks a lot in advance!

Well, the error message says that you can merge maps (aka dict). But you’re probably (not enough context posted to really tell) using the current page (the “dot” you’re passing as the first parameter to merge).

Hi, thanks for your reply. Yes, the OpenGraph template apparently expects the current page as a parameter: https://gohugo.io/templates/internal/ So if I could merge additional values into the .Params dict, how to update that one in the Page variable?

That may well be the case. But the error you mentioned comes from a wrong usage of the merge function.

You might want to write your own OpenGraph template if the one delivered with Hugo doesn’t fit your needs. Passing some invented parameter to this one doesn’t solve your problem.

Yeah, I understand where the error is coming from. However, my question is not about this error, but how to use the OpenGraph template with a different parameter for images. But apparently this is impossible?

You can specify images in the front matter, if I understand the documentation correctly. But you can’t pass a self-invented data structure to a template expecting a page object.

So hugo simply isn’t flexible enough to manipulate the page data structure? This is static and cannot be changed besides editing all past articles manually?

This argument is getting you nowhere. page is an internal data structure, and why would or should the Hugo developers allow templates to manipulate that? The next person wants to remove a part of this structure, because they believe that they don’t need it…

If you do not like the behavior of the pre-defined template, write your own. You can use the pre-defined one as a starting point and modify its parameter list in every which way you like.

Okay, thanks. All you’re telling me is I’m doing it wrong, providing not enough context, using the merge method in the wrong way, using a “self-invented data structure”… I don’t think that trying to use existing code (the OpenGraph template) instead of reinventing the wheel is the wrong way. But apparently this isn’t possible, so yeah, probably I’ll have to maintain a copy of that template now.

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