HUGO

How to Range .Site.Data Key/Value Where Equal to Front-Matter Key/Value Range

The title example is probably why I am confused to making this work - lol

I am trying to retrieve values from .Site.Data.contributors only where they match to the front matter contributor.name .

Hopefully the code bellow makes more sense(?).

The Front Matter:

+++

[[contributor]]

name = "John Doe"

[[contributor]]

name = "Jane Doe"

+++

/data/contributors.toml:

[John-Doe]
name            = "John Doe"
role            = ["author","editor"]
guid            = "7ce177def422404391300b286481f157"
bio             = "This is John Doe's Bio"
avatar          = "john-doe.jpg"
avatarCaption   = "A nerd"
links           = [
    "https://www.instagram.com/.../",
    "https://500px.com/p/...",
    "https://www.linkedin.com/in/.../"
]

[Jane-Doe]
name            = "Jane Doe"
role            = ["author","editor"]
guid            = "025c964413724f7fa7b402c283b74ee9"
bio             = "This is Jane Doe's Bio"
avatar          = "jane-doe.jpg"
avatarCaption   = "Also a nerd"
links           = [
    "https://www.instagram.com/.../",
    "https://500px.com/p/...",
    "https://www.linkedin.com/in/.../"
]

[Grease-Monkey]
name            = "Grease Monkey"
role            = ["author","editor"]
guid            = "025c964413724f7fa7b402c283b74ee9"
bio             = "This is Grease Monkey's Bio"
avatar          = "grease-monkey.jpg"
avatarCaption   = "Also a nerdy-monkey"
links           = [
    "https://www.instagram.com/.../",
    "https://500px.com/p/...",
    "https://www.linkedin.com/in/.../"
]

The HTML:
NOTE: .name parses correctly but everything else seems to returns nul

    {{- $baseURL	:= substr .Site.BaseURL 0 -1 -}}
	{{- $imgCDN     := .Site.Params.cloudinaryURL -}}
	{{- $avatarSRC  := printf "%s%s" $imgCDN "/static/avatars/" -}}

	{
		"@type" : "contributor",
		"person": [
			{{ range .Params.contributor }}
			{{- $data			:= $.Site.Data.contributors -}}
			{{- $contributorPath:= printf "%s%s" $baseURL "/about/contributors/" -}}
			{{- $name      		:= replace .name  " " "-" -}}
			{{- $contributorURL	:= printf "%s%s" $contributorPath $name -}}
			{{- $GUID 			:= printf "%s%s" "/#/schema/person/" .guid }}
			{
				"@type" : "Person",				
				"@id"   : "{{- printf "%s%s" $baseURL $GUID }}",
				"name"  : "{{- .name -}}",					
				"url"   : "{{- $contributorURL -}}",
				"image"	: {
					"@type"			: "ImageObject",
					"@id"			: "{{- printf "%s%s" $contributorURL "/#avatar" -}}",
					"contentUrl"	: "{{- printf "%s%s" $avatarSRC .avatar -}}",
					"caption"		: "{{- .avatarCaption | safeHTML -}}",
					"inLanguage"	: "en-US"
				},
				"description": "{{- .bio | safeHTML -}}",
				"sameAs" : [
					{{- range $comma, $e := .links -}}
					{{- if $comma -}},{{- end }}
					"{{ . }}"{{- end }}
				]
			}{{ end }}
		]
	}

Currently Outputs as: :roll_eyes:

...
},{
		"@type" : "contributor",
		"person": [
			{
				"@type" : "Person",				
				"@id"   : "https://stageing.xyz.com/#/schema/person/%!s(<nil>)",
				"name"  : "John Doe",					
				"url"   : "https://stageing.xyz.com/about/contributors/John-Doe",
				"image"	: {
					"@type"			: "ImageObject",
					"@id"			: "https://stageing.xyz.com/about/contributors/John-Doe/#avatar",
					"contentUrl"	: "https://res.cloudinary.com/xyz/image/upload/static/avatars/%!s(<nil>)",
					"caption"		: "",
					"inLanguage"	: "en-US"
				},
				"description": "",
				"sameAs" : [
				]
			}
			{
				"@type" : "Person",				
				"@id"   : "https://stageing.xyz.com/#/schema/person/%!s(<nil>)",
				"name"  : "Jane Doe",					
				"url"   : "https://stageing.xyz.com/about/contributors/Jane-Doe",
				"image"	: {
					"@type"			: "ImageObject",
					"@id"			: "https://stageing.xyz.com/about/contributors/Jane-Doe/#avatar",
					"contentUrl"	: "https://res.cloudinary.com/xyz/image/upload/static/avatars/%!s(<nil>)",
					"caption"		: "",
					"inLanguage"	: "en-US"
				},
				"description": "",
				"sameAs" : [
				]
			}
		]
	}]

The debug for $.Site.Data.contributors:

	map[string]interface {}{"Jane-Doe":map[string]interface {}{"avatar":"jane-doe.jpg", "avatarCaption":"Even Nerdier", "bio":"This is Jane Doe's Bio", "guid":"c4ec735a3eaa4e5eb1f6ef23deb03931", "links":[]interface {}{"https://"}, "name":"Jane Doe", "role":[]interface {}{"founder", "photographer", "author", "editor"}}}}]

Except .name, all the data field came from {{- $data := $.Site.Data.contributors -}}, then you must call it like this $data.<key>.

Yea, thats why I had the {{- $data := $.Site.Data.contributors -}} in there; Unfortunately it does not work – returns the same nil.

whoops, my bad,
it’s because the $data not calling correct contributor map yet.

Need to call correct contributor map data using index:

{{ range .Params.contributor }}
{{- $name      		:= replace .name  " " "-" -}}
{{- $data			:= index $.Site.Data.contributors $name -}}

....
{{ end }}
   {{- $baseURL	:= substr .Site.BaseURL 0 -1 -}}
	{{- $imgCDN     := .Site.Params.cloudinaryURL -}}
	{{- $avatarSRC  := printf "%s%s" $imgCDN "/static/avatars/" -}}

	{
		"@type" : "contributor",
		"person": [
			{{ range .Params.contributor }}
++          {{- $name      		:= replace .name  " " "-" -}}
++			{{- $data			:= index $.Site.Data.contributors $name -}}
			{{- $contributorPath:= printf "%s%s" $baseURL "/about/contributors/" -}}
--          {{- $name      		:= replace .name  " " "-" -}}			
			{{- $contributorURL	:= printf "%s%s" $contributorPath $name -}}
--			{{- $GUID 			:= printf "%s%s" "/#/schema/person/" .guid }}
++			{{- $GUID 			:= printf "%s%s" "/#/schema/person/" $data.guid }}
			{
				"@type" : "Person",				
				"@id"   : "{{- printf "%s%s" $baseURL $GUID }}",
				"name"  : "{{- .name -}}",					
				"url"   : "{{- $contributorURL -}}",
				"image"	: {
					"@type"			: "ImageObject",
					"@id"			: "{{- printf "%s%s" $contributorURL "/#avatar" -}}",
--					"contentUrl"	: "{{- printf "%s%s" $avatarSRC .avatar -}}",
++					"contentUrl"	: "{{- printf "%s%s" $avatarSRC $data.avatar -}}",
--					"caption"		: "{{- .avatarCaption | safeHTML -}}",
++					"caption"		: "{{- $data.avatarCaption | safeHTML -}}",
					"inLanguage"	: "en-US"
				},
--				"description": "{{- .bio | safeHTML -}}",
++				"description": "{{- $data.bio | safeHTML -}}",
				"sameAs" : [
--					{{- range $comma, $e := .links -}}
++					{{- range $comma, $e := $data.links -}}
					{{- if $comma -}},{{- end }}
					"{{ . }}"{{- end }}
				]
			}{{ end }}
		]
	}

This is a good example where Variable-Order counts:

What @pamubay had for an answere wasn’t quite there:

{{ range .Params.contributor }}
{{- $name      		:= replace .name  " " "-" -}}
{{- $data			:= index $.Site.Data.contributors $name -}}
{{- $contributorPath:= printf "%s%s" $baseURL "/about/contributors/" -}}
{{- $contributorURL	:= printf "%s%s" $contributorPath $name -}}
{{- $GUID 			:= printf "%s%s" "/#/schema/person/" $data.guid }}

However, this order does: :thinking:

{{- $contributorPath:= printf "%s%s" $baseURL "/about/contributors/" -}}
{{- $name      		:= replace .name  " " "-" -}}
{{- $contributorURL	:= printf "%s%s" $contributorPath $name -}}
{{- $data			:= index $.Site.Data.contributors $name -}}
{{- $GUID 			:= printf "%s%s" "/#/schema/person/" $data.guid }}

Thnx!

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