How to lookup for a specific item inside a map or array?

I have, in my content files, defined (in the front matter) a custom property:

- name: MSFT
- name: GOOG

Inside my content files, I want to be able to create a short code that simply looks up the myresources param for a passed name and returns the link.

This implies that I need to search inside resources for an entry that satisfies a condition.

Something like this in my shortcode myres.html:

{{ . | find <condition> }}

And I would use it this way in a content file:

Go to this link: `r blogdown::shortcode("myres", .Params.myresources)`

How to do that?

First of all I think that resources is a reserved term that will behave different - it’s part of the Page Resources functionality.

Call it tickersource or something like that.

You can “range” through maps/arrays with the range function:

{{ range .Params.tickersources }}
    {{ .name }} - {{ .link }}
{{ end }}

should do the trick. (not tested)

Yes, it is a reserved term, in fact I changed name :slight_smile:

But unfortunately this is not the solution to my problem. I don’t wanna list the terms… I wanna get only one basing on one condition! I have edited my question to make my intentions more evident.

You can use where to look for specific items in an array

1 Like

Have a look at index. It will fetch you the value for a specified key of a map. That said, you might want to reorder that database as a map, with GOOG, etc. being the keys.

You can always have nested maps.

more samples here

(again, untested, could contain errors)
The way I understand what you want is this:

Your front matter:


Your shortcode: {< ticker name="MSFT" >}. Then in the shortcode template, you do your lookup:

{{ .Params.myresources.( .Get "name" ).Link }}

I am not sure about the ( .Get "name )-part. this would be my first try and depending on the error message I would look for the right term. .Get is your shortcode parameter getting function.

Another way to get there would be {{ index .Params.myresources "MSFT" }} and you might use it with with and then inside the reference . would point to the slice, so .Link points to