Images in Hugo RSS?

I found this is helpful: https://www.calhoun.io/tag/templates/

How do I translate this: https://www.socketloop.com/tutorials/golang-how-to-check-if-a-string-contains-another-sub-string

Into the template?

This attempt does not work:

<media:content type="{{ if strings.Contains(.Params.banner, "png") }}image/png{{ else }}image/jpeg{{ end }}" medium="image" width="700" height="400" url="https://cloudywindows.com/{{ .Params.banner | safeHTML }}" />

Hmmm - wish this was in native GO: https://github.com/grammarly/rocker/pull/23/files

This feels really close:

{{ if and (isset .Params "banner") (not (eq .Params.banner "")) }} <media:content type="{{ if (call .strings.Contains .Params.banner "png" ) }}image/png{{ else }}image/jpeg{{ end }}" medium="image" width="700" height="400" url="https://cloudywindows.com/{{ .Params.banner | safeHTML }}" /> {{ end }}
but hugo gives this error:

ERROR 2017/04/22 11:23:55 Error while rendering "Announcement": template: rss.xml:22:47: executing "rss.xml" at <.strings.Contains>: can't evaluate field strings in type *hugolib.Page

@spf13 - could you please give me guidance on whether and how I would address a go function like .strings.Contains this from within hugo’s framework?

Thankyou all this forum really good and nice

Hey mate, can you paste row 22?

Line 22 is the one in the code snippet in the same post that starts with <media:content…

I know the error is from not addressing the function call correctly => this code:

if (call .strings.Contains .Params.banner "png" )

Seems like I just need to know how to directly call a function of go in a template. Or maybe I need to know that you cannot do so unless other code modification are done.

What is it you want to accomplish with that line of code?

If you just want to check if .Params.banner contains “png”, then write

{{ if in .Params.banner "png" }}

Thanks @Rick for all your help. Here is my working code modification to an external rss.xml template. I have also included my tweak to change the description to a summary (discussed in another thread).

<description>{{ .Description | default .Summary | html }}</description>

{{ if and (isset .Params "banner") (not (eq .Params.banner "")) }}

<media:content type={{ if in .Params.banner "png" }}"image/png"{{ else }}"image/jpeg"{{ end }} medium="image" width="700" height="400"
url="https://cloudywindows.com/{{ .Params.banner | safeHTML }}"></media:content>

{{ end }}

No worries mate, glad I could help!

Thanks to both of you. I’m redoing the feed for my personal site and launching a new site soon that needs a “modern” feed. This will be very helpful. Bookmark’d. :wink:

Don’t mention it! Glad we/I could help :blush:

I just don’t get why Hugo’s internal RSS template doesn’t support images out of the box.

Why {{ .Content }} skips the images in the RSS template?

Why do we have to use yet another frontmatter parameter to get this working?

EDIT [21.7.2017]
I tested this further and here is what I found:

If one uses Hugo’s internal RSS template the feed is generated only with post titles (no text body and no images).

If one copies Hugo’s RSS template from https://gohugo.io/templates/rss/#the-embedded-rss-xml

And pastes its contents with no changes into the following path /layouts/rss.xml

Then the contents of a post are rendered in full including images.

Why is that @bep ?

Fyi to all, my feed would not validate without a trailing slash on this reference:

xmlns:content="http://purl.org/rss/1.0/modules/content"

You may get some inspiration from this template:

HI, here is my template. I changed all to the page bundle. The first picture from the sub dir ./images will used for the feed. Change my template to get the picture size you like or use a fix file name.
Use .Site.Params.feedLimit to set the max number of entries.

<feed xmlns="http://www.w3.org/2005/Atom">
    <title>{{ if .IsHome }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} auf {{ end }}{{ .Site.Title }}{{ end }}</title>
    <link rel="self"  type="application/atom+xml" href="{{.Permalink}}feed.xml"   />
    <link rel="alternate" type="type/html" href="{{.Permalink}}" />
    <generator>Hugo - Version {{ .Hugo.Version }}</generator>
	{{ with .Site.Author.name }}<author>
		<name>{{.}}</name>
		{{ with $.Site.Author.email }}<email>{{.}}</email>{{end}}
	</author>{{end}}
	{{ with .Site.Copyright }}<rights>{{.}}</rights>{{end}}
	{{ if not .Site.LastChange.IsZero }}<updated>{{ .Site.LastChange.UTC.Format "2006-01-02T15:04:05Z07:00" | safeHTML }}</updated>{{ end }} 
	<id>{{.Permalink}}</id>

    {{ range first .Site.Params.feedLimit .Data.Pages }}
	{{ if ne .Section "" }}
    <entry>
	  {{ $seite := .Permalink }}
      <title>{{ .Title }}</title> 
      <link rel='alternate' type='type/html' href='{{ $seite }}' />
      <updated>{{ .Date.UTC.Format "2006-01-02T15:04:05Z07:00" | safeHTML }}</updated>
      <id>{{ .Permalink }}?id={{ md5 .Content }}</id>
      <summary type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml">
		{{ range first 1 (.Resources.Match "images/*.*") }}{{ $original := . }}{{ $.Scratch.Set "image" ($original.Fit "480x360") }}{{ $image := $.Scratch.Get "image" }}
		<a href='{{ $seite }}'><img  src="{{ $image.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" /></a><br/><br/>
		{{ end }}
		{{ .Summary | plainify }}
		<br/><a href='{{ $seite }}'>[hier lesen]</a>
		</div>
		</summary>
    </entry>
    {{ end }}
	{{ end }}
</feed>

Hope this helps. Replace “feed.xml” with the parameters fom config file … or use a fixed one. This should be the same in the META directives in your pages.

I am so close with @bep 's example.

I am having trouble with the image lookup.

My images are in my front matter as such:

banner = "banners/PowerShellReaper.png"

Where “banners” is a subfolder under “static”.

In my kinda working template I reference it like this:

url="https://cloudywindows.io/{{ .Params.banner | safeHTML }}"

How do I change this so that it can work for this line in @bep’s example:

{{ $img := (.Resources.ByType "image").GetMatch "*featured*" }}

Thanks for any help!

Learning this templating language is the hardest thing about hugo. But most especially when you only need to know this language for the few times you have to make changes to things like the rss template.

I have combined the above statements, but now have no expansion of template statements in the cdata block.

After a lot of head banging trial and error, the below works - but I have no idea how sorry of a hack it is.

I also wish I could have retained the image resizing of the original but don’t know how to assign .Params.banner to a variable that has the same meta data as the original and acts like an object.

{{ if and (isset .Params "banner") (not (eq .Params.banner "")) }}
  {{ printf "<![CDATA[<img src=\"https://cloudywindows.io/%s\" width=\"70\" height=\"40\"/>]]>" (.Params.banner) }}
{{ end }}
{{ .Content | html }}
1 Like