Shortcode parameter in if statement [solved]

I have a shortcode that I want to pass in a parameter and then only show items in a data file if they have the matching parameter in one of the data items.

I have a data file that has items like:

- title: cool item
  url: someurl
  featured: core

In the shortcode I want to pass in core and then only show those items that have featured = core.

I have tried this:

 {{ range .Site.Data.products.items }} 
          {{ if eq .featured (.Get 0) }}

         //do cool stuff here

         {{ end }}{{ end }}

But I get an error about Get is not a method but has arguments.

If I just hard code the if part as {{ if eq .featured "core" }} it works great. Likewise if I put {{.Get 0}} outside the if I can get core.

How can I get the comparison to use the parameter? It seems like I need to assign .Get 0 to a variable and then use that in the if statement but I don’t see anything in the docs about that - though I did read about Scratch and that sounded close but I didn’t see an example that made any sense to me.

Did you try where? See here: Where Function. It might be possible to range where .Site.Data.products.items.featured "core". Didn’t try but hopefully it’s the right path.

my code works fine if I hard code the core part, trying to get it to work where core is passed in via a parameter so the comparison is against whatever the parameter is which is where I am stuck.

well that was easy, scratch to the rescue.

Here is what works:

  {{ $.Scratch.Add "servicetype" (.Get 0) }}
      {{ range .Site.Data.products.items }}
      {{ if eq .featured ($.Scratch.Get "servicetype") }}
          <div class="service-flexitem">
           <a href="{{.url}}">
             <img alt="" src="/assets/img/icons/{{.icon}}" class="img-responsive-hm">

           </a>
           <div class="service-text">
             <a class="servicea" href="{{.url}}"><h3 class="serviceh2">{{.title}}</h3></a>
             <p>{{.description}}</p>  
           </div>
         </div>
         {{ end }}{{ end }}
2 Likes