Getting title for an alternate output format

I would like the RSS feed links on my site to include a title. I tried adding a feed link like this:

    {{ with .OutputFormats.Get "RSS" }}                                     
    <link href="{{ .RelPermalink }}"                                        
          type="{{ .MediaType.Type }}"                                      
          title="{{ .Title }}">                                             
    {{ end }}                                                               

This fails with several errors along the lines of:

ERROR 2019/05/06 13:30:49 render of “section” failed: execute of template failed: template: post/list.html:1:3: executing “post/list.html” at <partial “header.html” .>: error calling partial: “/…/themes/ghostwriter/layouts/partials/header.html:30:24”: execute of template failed: template: partials/header.html:30:24: executing “partials/header.html” at <.Title>: can’t evaluate field Title in type *page.OutputFormat

I thought maybe this was just a question of .Title being unset in some contest, so I tried this instead:

          title="{{ .Title | default "" }}">                                             

…but that resulted in the same errors. How do I get the page title into this link?

Hey there,

An Output Formats retrieve with .OutputFormats.Get is an Output Format, .Title is available on pages, not on Output Format.

If your page is the root context of your template file then try $.Title instead.

1 Like

Thanks, $.Title worked!

1 Like