Can I use ".Summary" variable in header?

I’m trying to set something up so that on index or list pages on my site, I would have the meta description tag populated with the site description as set in config.toml

<meta name="description" content="mysitedescription">

but on individual post pages I would have the tag populated with the content of the .Summary variable

<meta name="description" content=".Summary">

Getting the generic site description is easy enough, but I’m not having any joy accessing the .Summary variable from within header.html. Here’s what I’ve tried so far, with no luck:

<meta name="description" content="{{ if .Data.Pages.Summary }}{{ . }}{{else}}{{ .Site.Params.mysitedescription }}{{end}}">

Trouble is that, as with all variable access in Hugo, I’m finding the whole thing tantamount to voodoo.

For a start I’m not entirely sure whether the post .Summary variable is even available within the header section [Depending on how Hugo actually constructs pages, it may not exist yet, when the header section is being written].

And secondly it’s another one of those “How do I actually actually access this Variable anyway?” puzzlers. The only example I’ve got to go on is where it’s accessed on the Index page as part of the {{ range .Data.Pages }} loop, which is what I tried to work from above, but to no avail.

How would I [if I can] access the .Summary for a single post, within the single post page itself?

The important issue in this case is to know the distinction between Node and Page.

Nodes (list pages, index page) have no summary.

Something like this should work:

{{if .IsPage}}{{ .Summary }}{{else}}{{.Site.Params.description}}{{end}}

Thanks for taking the time to reply. Unfortunatey that didn’t work either:

Rendering error: template: theme/partials/header.html:9:40: executing "theme/partials/header.html" at <.IsPage>: IsPage is not a field of struct type *hugolib.Node

That was a copy-paste from my working site:

So, it works for me … Something weird is happening here, as IsPage is definitively a field on Node.

Some more context needed here, like Hugo version.

Is it necessary to know whether it is a page or node. Isn’t it enough to know whether .Summary exists or not. If it doesn’t exist then either it is a node or a page without a summary. Either way fall back on the site description.

{{ with .Summary }}{{ . }}{{ else }}{{ .Site.Params.description }} {{ end }}

That construction will give

Summary is not a field of struct type *hugolib.Node

On nodes.

@bjornerik thanks for that. I learn something new everyday. I would simply have expected the function to return false. That is what happens when one is used to thinking in php. I tried the code you posted and can confirm no problems with either 0.12 or 0.13dev (based on todays current codebase).

1 Like

Well, this is bizarre. I’ve just copy/pasted the line directly from your header.html file, just in case I’d made a typo before, and I’m still getting the same error:

ERROR: 2014/12/14 Rendering error: template: theme/partials/header.html:10:40: executing "theme/partials/header.html" at <.IsPage>: IsPage is not a field of struct type *hugolib.Node

Here is my header.html partial:

<html lang="en-ie">
{{"<!-- begin header partial //-->" | safeHtml}}
<head> 
    {{ "<!-- site title and article title [title-cased] -->" | safeHtml}}
    <title>{{ .Site.Title }} | {{ title .Title }}</title>
    {{"<!-- metatags //-->" | safeHtml}}
    <meta charset="{{ .Site.Params.mdrcharset }}">
    <meta name="author" content="{{ .Site.Params.mdrauthor }}">
    <meta name="description" content="{{if .IsPage}}{{ .Summary }}{{else}}{{.Site.Params.mdrdescription}}{{end}}">
    <meta name="copyright" content="{{ .Site.Params.mdrcopyright }}">
    <meta name="keywords" content="{{ if .Keywords }}{{ range .Keywords }}{{ . }}, {{ end }}{{else if isset .Params "tags" }}{{ range .Params.tags }}{{ . }}, {{ end }}{{end}}">
    <link rel="canonical" href="{{ .Permalink }}">
      {{ "<!-- CSS //-->" | safeHtml }}
      <link rel="stylesheet" href="{{ .Site.BaseUrl }}/css/stiobhart.css">
      {{"<!-- CDN hosted highlight.js lib //-->" | safeHtml}}
      <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/monokai.min.css">
      <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
      <script>hljs.initHighlightingOnLoad();</script>
    {{ "<!-- RSS feed-->" | safeHtml }}
    {{ if .RSSlink }}<link href="{{ .RSSlink }}" rel="alternate" type="application/rss+xml" title="{{ .Title }}" />{{ end }}
</head>
{{"<!-- end header partial //-->" | safeHtml}}````

This is weirdo.

Whats the output of

hugo version

?

1 Like

The only difference i can see is that, judging from the structure of your repo, your header.html is in /layouts/partials, whereas mine is in /themes/mythemename/layouts/partials. Could that make any difference, I wonder?

My Hugo version is: v0.11

I’m pretty sure its a version issue.

Current release version is 0.12. The documentation is written for 0.12 - and my guess IsPage was introduced in 0.12.

0.13 is in development.

Ah. OK. Time to try an upgrade…

[UPDATE]

That did the trick. All working as expected now. Cheers for the help.

I think what happened was I originally installed Hugo from source, then later ‘updated’ via Homebrew, but forgot to fix the symbolic links so, although I thought I had the latest version installed, the ‘Hugo’ command was still linking against the earlier version binary.

1 Like