news
news

Added support for using post categories in email newsletter templates.

|
Embed
Progress spinner
vladcampos
vladcampos

@news πŸ‘

|
Embed
Progress spinner
In reply to
otaviocc
otaviocc

@news does it work for existing newsletters (in case I want to change the template and re-send it myself to try?) Is it .Pages.Params.categories?

|
Embed
Progress spinner
otaviocc
otaviocc

@news I'm getting "Newsletter: Error running template, Error parsing template: template: template:53: function "in" not defined". If it's .Pages.Params.categories, is it an array?

|
Embed
Progress spinner
otaviocc
otaviocc

@news it didn't work with in as it did for blog posts, but if I use range I can iterate over the categories and check if the one I need is there. Thanks!

|
Embed
Progress spinner
manton
manton

@otaviocc I'm updating the help page about this. For each blog post in .Pages, there should be .Params.categories now. Let me know if that matches what you're seeing.

|
Embed
Progress spinner
otaviocc
otaviocc

@manton exactly, as I mentioned I used range to access it. I noticed is that the same code from a blog post doesn’t work for newsletters. Take the following example which works for posts:

{{ if .Params.categories }}
    {{ if in .Params.categories "Article" }}
        πŸ”—
    {{ else if in .Params.categories "Video" }}
        πŸŽ₯
    {{ else }}
        πŸͺ΄
    {{ end }}
{{ end }}

When the code above is used in newsletters, the compiler complains that the function in (doc) is not present. The solution, in newsletters, is to use range (doc) to iterate over all the categories for that particular post and check if the category matches the criterion.

{{ if .Params.categories }}
    {{ range .Params.categories }}
        {{ if eq . "Article" }}
            πŸ”—
        {{ else if eq . "Video" }}
            πŸŽ₯
        {{ else }}
            πŸͺ΄
        {{ end }}
    {{ end }}
{{ end }}

Maybe newsletters are processed outside Hugo? And in is implemented by Hugo and not by Go?

|
Embed
Progress spinner
manton
manton

@otaviocc Yes, newsletters are processed by a custom Go program I wrote, not Hugo. I wonder if β€œin” is implemented by Hugo. I’d love for this to be as consistent as possible. Thanks for testing!

|
Embed
Progress spinner