Summary on home page

Can we have an option where I continue to use {{ .Content }} and yet have an option to truncate posts at the point where I specify the <!--more--> tag in my posts?

Using summary truncates all posts on the homepage.

As written, this is false.

The .Content of your home page is determined by what appears beneath front matter in content/

The .Summary of your home page is determined by the method used to define it.

Regardless of the method used to define the .Summary, it will only include content from content/

I see what you mean. Thanks for the extra information that helped me understand better.

This is what I have in my /layouts/index.html:

{{ define "main" }}
	{{ $posts := where site.RegularPages "Type" "post" }}
	{{ $grouped := $posts.GroupByDate "2006-01-02" }}
	{{ $paginated := (.Paginate ($grouped)) }}
	<main class="content" role="main">
		{{ range $paginated.PageGroups }}
		{{ $thedate := (time .Key) }}
		<h3 class="post-group"> {{ $thedate.Format "January 2, 2006" }}</h3>
  		{{ range .Pages.Reverse }}
    	{{ if eq .Type "post" }}
      		{{ .Render "article" }}
    {{- end }}
  {{ end }}
{{ end }}
{{ partial "pagination.html" . }}
{{ end }}

So I then head to the /layouts/post/article, I have this:

<article class="h-entry post">
	<header class="post-header">
    {{ with .Params.image }}
      <img class="u-featured" src="{{ . }}"></img>
    {{ end }}
		<h2 class="p-name post-title"><a href="{{ .URL }}">{{ .Title }}</a></h2>
	<section class="e-content post-content">
		<a href="{{ .Permalink }}" class="u-url read-more"><span class="dt-published" datetime="{{ .Date.Format "2006-01-02T15:04:05-0700" }}">{{ .Date.Format "03:04 PM" }} ▸</span></a>
      {{ .Content }}
          {{ if .Truncated }}
              <p><a href="{{ .RelPermalink }}">Read More</a></p>
          {{ end }}
	<footer class="post-meta">

I added the code below after {{ .Content }}. As you predicted, the truncate code below doesn’t work. It does work when I use {{ .Summary }} but that also “summarizes” all posts regardless of <!--more-->

{{ if .Truncated }}
              <p><a href="{{ .RelPermalink }}">Read More</a></p>
          {{ end }}

The /layouts/post/summary.html does have {{ .Content }} as the body so I’m not sure how Summary is being determined.

Your problem statement and desired outcome are difficult to understand. Can you provide a link to the public repository for your project? It would save some time…

Also, you reference “/layouts/post/summary.html” but that isn’t called anywhere in your example.

Here you go. New to Hugo so still figuring out stuff.

When providing a repository for others to clone and build, please create a complete site (valid configuration, sample content, it builds, etc.). Thanks.

EDIT: I expect the following to work:

git clone --recurse-submodules
cd musings
hugo server

Ok. But that’s not my repository. I’m just using the theme.

The expectation is a site that we can build, not just a piece of it.


Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

Ok. Let me restate my problem and desired outcome. I’m looking to feature full-length posts on my homepage but for some posts, I want them truncated at the point when I specify the tag.

Method 1

This approach essentially disables automatic summary splitting on your site. In your site configuration:

summaryLength = 999999 # Tolstoy's War and Peace has around 600000 words.

Then in your template you can do:

{{ .Summary }}

This will return a stripped version of .Content unless you (a) manually split the summary with the <!--more--> tag, or (b) define the summary in front matter.

Method 2 (recommended)

Parse the raw content for the <!--more--> tag.

{{ if or (findRE `<!--more-->` .RawContent) .Params.Summary }}
  {{ .Summary }}
{{ else }}
  {{ .Content }}
{{ end }}

This will return .Content unless you (a) manually split the summary with the <!--more--> tag, or (b) define the summary in front matter.


Much appreciated. The second method worked!

I’ll figure out where to add the summaryLength = 999999 for the first method.

Actually, don’t use Method 1. I forgot about this issue. Tags are stripped.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.