From shortcode check if article is on single or on home page

Blog consists of typical articles:

  • headline
  • credits (showing from shortcode)
  • text

That are paginated on home page:

  • headline
  • credits (showing from the same shortcode)
  • truncated text with “read more”

I need to inject some differences to credits markup when showing on home page (in articles list). From shortcode how can I get current page object to see it’s .IsHome variable?

Please post the range block from your home page template. As always, access to the entire project will save everyone’s time.

Home page is generated with this list.html template

{{ $pag := .Paginate (where .Site.RegularPages "Type" "in" (slice "bio" "documentary")) }}

{{ range $pag.Pages }}
<article>
    <h2><a href="{{.RelPermalink}}">{{ .Title | safeHTML }}</a></h2>
    {{ .Summary }}
    {{ if .Truncated }}
    {{ partial "read-more" . }}
    {{ end }}
</article>
{{ end }}
{{ partial "pagination" . }}

Each article contains block of credits showing with this shortcode:

{{ $a := site.GetPage (printf "authors/%s" (index .Page.Params.authors 0)) }}
{{ $addon := .Get 0 }}
<div class="credits">
    <div class="author"><a href="{{ $a.Params.personalUrl }}" target="_blank" rel="nofollow noopener">{{ $a.Params.fullname }}</a>
    <time datetime="{{ .Page.Params.date.Format `2006-01-02T15:04:05` | safeHTML }}"></div>
    <div class="addon">{{ $addon | safeHTML }}</div>
</div>

The task is don’t show addon block of credits when listing articles on home page

This isn’t possible. When Page A (outer) renders Page B (inner), the inner page does not have access to the outer context. Or to put it another way, Page B knows that it is being rendered, but it doesn’t know who is doing the rendering.

You will need to restructure your templates.

You could use Content Views (with .Render). It’s the way I solved a similar problem with my Hugo site.

Yeah, some trick with custom views seems to be a workaround, but I’m going to refactor templates as Joe recommends. Because initial idea of credits integration to article’s body via shortcode was bad in meaning of project architecture. So my bump into problem described above was naturally inevitable.

Now I’m going to move addon block to front matter and render credits from template, not from article.

1 Like

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