[SOLVED] If isset .Params not working

The code below displays related posts in ‘person/single.html’ if ‘.Params.name’ in a ‘person’ content type matches ‘.Params.person’ in a ‘post’ content type. What I’m trying to do now is to add a “No related posts” message if there are no related posts for a person. I tried:
{{ if isset .Params "person }} before the range, but it didn’t work. Her’s a snippet of my code:

content/post/2018-01-18-john-smith.md
title: Post title here
person: John Smith
summary: lorem...

content/person/john-smith.md
name: John Smith
bio: lorem...

…\themes\mytheme\layouts\person\single.html

   // get person name
    {{ $person := .Params.name }}

  // get posts
    {{ $posts := where .Site.Pages "Section" "post" }}

  // find all posts where .Params.person (in 'post') matches .Params.name (in 'person')
  {{ $relatedPosts := where ($posts) ".Params.person" "=" .Params.name }}
  <ul>
      {{ range (where $relatedPosts ".Params.person" "ne" nil) }}        
          {{ partial "components/post-list-item.html" . }}                                      
      {{ end }} 
  </ul>

It should not be both existing and not existing so, I wonder, would putting another range statement between the <ul> tag work?

<ul>
      {{ range (where $relatedPosts ".Params.person" "ne" nil) }}        
          {{ partial "components/post-list-item.html" . }}                                      
      {{ end }} 
</ul>
<ul>
      {{ range (where $relatedPosts ".Params.person" "eq" nil) }}        
          <span>No related posts</span>                                      
      {{ end }} 
</ul>

???

Maybe need {{ if .Params.person }}?

Ok, thanks for the help @RickCogley & @Mikhail ; it still didn’t work. For some reasons, it looks like the problem is when.Params.person is not set. For the code below: if both .Params.person and .Params.name are set, it prints TEST: Related news found - if .Params.person is not net, it doesn’t print anything.

{{ range $relatedPosts }}
    {{ if .Params.person }} 
        <p>TEST: Related news found</p>
        <ul>
            {{ partial "components/post-list-item.html" . }}
        </ul>
    {{ else }}
        <p>No related news.</p>
    {{ end }}                                  
{{ end }}

Is there a way to check if a Param doesn’t exist in frontmatter at all. (not if it’s not set or empty)?

Works with .Site.RegularPages .

<!-- set posts -->
{{ $posts := (where .Site.RegularPages ".Params.person" "=" .Params.name) }}

<!-- set post counts -->
{{ $postCount := len $posts }}

<!-- if post count is > 0, find all posts where .Params.person in 'post' type matches .Params.name in person type -->

{{ if gt $postCount 0 }}    	
	{{ $relatedPosts := where ($posts) ".Params.person" "=" .Params.name }}        

	{{ range $relatedPosts }}
		<ul>
			{{ partial "components/post-list-item.html" . }}
		</ul>                   
	{{ end }}
{{ else }}
	<p>No related news.</p>
{{ end }}
1 Like

Thanks for posting the solution @kenold.

1 Like