Access Data File Params as Variable

Hi there

I have a data file with some variables

  • name: ‘abc’
    attract:
    convert: true
    close:
    retain:
  • name: ‘xyx’
    attract:
    convert:
    close: true
    retain:

In a single template, i get the corresponding front matter .Params
{{ $key := .Title | urlize }}

But this returns all the records and not the corresponding ones:

{{ range  $data.modules  }}
{{ if $key }}
{{ .}}
{{ end }}
{{ end}}

Why?

If I replace {{ if $key }} by {{ if .attract}} for example it works.

Did I miss something ?

{{ if $key }} is always true because $key is never an empty string. You need to compare $key to something like this:

{{ if eq .name $key }}

Alternatively consider using with

        {{ if eq .attract true }}

will work.
But

         {{ $key := ".attract" }}
        {{ if eq $key  true }}

won’t work.

with rebinds the context, so how I can call other parameters inside the loop created ?

In the above you have turned .attract into a string.
Try setting the variable without the quotes.

Thanks guys.
the only solution that worked for me was

{{ $key := .Title | urlize }}
{{ range where $data.modules $key true}}

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