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
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?
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
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 }}
@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).
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}}````
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?
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.