Maps in frontmatter and "site.Param" (resolved)

Hi,
I have this variable in config:

[languages.fr.params.lettres_choix_gauche]
v = false
b = false

I can access it with site.Params, but I want to be able to overriding in frontmatters. I can’t find the syntax. What must I write and where ?
For now I have

+++
title = ‘Spécial pouce droit’
[lettres_choix_gauche]
v = true
b = false
+++
for instance, and

{{- if isset (site.Param "lettres_choix_gauche") $ch -}}
  {{- if index (site.Param "lettres_choix_gauche") $ch -}}{{ $isLeft = true -}}
  {{- else -}}{{ $isRight = true -}}
  {{- end -}}
{{- else -}}
  {{- errorf "File %s: char %q in text %q is not in left/right lists" $.Page.File.Filename $ch $.Text -}}
{{- end -}}

I tried with or default etc but can’t find the right combination. The aim being: find the table of letters, if the letter is not present throw error, assign to hand otherwise.

page.Param falls back to site value. something like that one

  • check if the value is set in site params
  • use fallback unless set on page
{{ range $ch := slice "a" "b" "v" }}
  {{- if isset site.Params.lettres_choix_gauche $ch }}
    <li>{{ $ch }} = {{ $.Param (add "lettres_choix_gauche." $ch) }}</li>
  {{ else }}
    <li>{{ $ch }} = nil</li>
  {{ end }}
{{ end }}

page frontmatter:

+++
title = "page"
[params.lettres_choix_gauche]
  v = true
+++

result:

site.Params

{
  "lettres_choix_gauche": {
    "b": false,
    "v": false
  }
}

page.Params

{
  "title": "Home (en)"
  "lettres_choix_gauche": {
    "v": true
  },
}

Result

  • a = nil
  • b = false
  • v = true

I want to default on the site.Params if the page param is not set, your code does the exact opposite. And using both isset and .Param (without s) seems utterly pointless.

With your given example where the site params are:

[languages.fr.params.lettres_choix_gauche]
v = false
b = false

and my frontmatter that only sets a value for v

[params.lettres_choix_gauche]
  v = true

The result for a is nil → because it’s not set in the site.Params
The result for b is false → because the value in site.Params is set to false and there is no value in frontmatter
The result for vis true → becase the value in site.Params is set to false but the value on the page is true and taken

Imho Pretty much “Take the page parameter if it is set or fall back to the site one”.
Except IF you are talking about taking the existance of the map lettres_choix_gauche? and take whatever is defined there?

Not really,

  • The first checks if the parameter is set in the site config
  • The second returns the value of the page param falling back to the sites one.

Your code will check the existence of a default setting before that of the page setting, if the default doesn’t exist, quit (nil), ignoring the page setting.
It doesn’t work, check for yourself
Try yourself: GitHub - evanescente-ondine/hugo-sample

the code assumes that the site config defines the DEFAULTS (and implicitly the available/allowed keys) so it tests if that is a valid key and use the local value if set and fall back to site value else.

… but let’s stop taking about my code which obviously does not match your requirements. Looks like I have a language barrier understanding these.

Is that what you want?

  • a map lettres_choix_gauche that can be defined
    • at page level (frontmatter)
    • at site level
  • each of these maps may define an arbitrary set of key=value pairs

if the map is defined on page level you want to ONLY use that map
if it is NOT defined on page level you want to use the map from the site config

then check if there is a key in the used map that is equal to $ch

and if yes set what to what (I’m missing the relation between $isLeft, $isRight and the values from the map …? maybe again some language barrier.

What happens if the map exists at page level, but the key is not present?

  1. if character not in $liste-gauche or $liste-droite check .Params.lettres_choix_gauche
  2. if present, use that value (set $isLeft if true, $isRight if false)
  3. if not check if present in site.Params.lettres_choix_gauche
  4. Repeat 2
  5. if not present, CRASHES

That sums it up :slight_smile:

come on - when did these pop up the scene :wink: did I again miss something… time to retire for today … seems by brain already sleeps

You’re not senile yet, I had not mentioned them because they come in before the code I showed. See for yourself in the repo (bio).

good morning, I cannot find $liste-gauche or $liste-droite or the term “bio” in you shared repo https://github.com/evanescente-ondine/hugo-sample

and I’m still unsure how your key=value maps, looks like you don’t do anything with the value if so, same logic using contains could be used.

but i found your link render hook :wink:

here’s coded for your hook that prints some values and check results,

guess a combination of these may do:

...
      {{ warnf "[ %s ]" $chars }}
      {{- range $chars -}}
        {{- $ch := . -}}
        {{- warnf "____[ %s ]         : === BEGIN =" $ch }}
        {{- $isLeft := in site.Params.list_left (lower $ch) -}}
        {{- $isRight := in site.Params.list_right (lower $ch) -}}
        {{- if not (or $isLeft $isRight) -}}
          {{- warnf "____[ %s ]         : SITE map defined?  : %v" $ch (isset site.Params "lettres_choix_gauche") }}
          {{- warnf "____[ %s ]         : SITE map content   : %v" $ch site.Params.lettres_choix_gauche -}}
          {{- warnf "____[ %s ]         : PAGE map defined?  : %v" $ch (isset $.Page.Params "lettres_choix_gauche") }}
          {{- warnf "____[ %s ]         : PAGE map content   : %v" $ch $.Page.Params.lettres_choix_gauche -}}
          {{- warnf "____[ %s ]         : --------------------" $ch }}
          {{- warnf "____[ %s ]         : SITE map value     : %s = %v" $ch $ch (index site.Params.lettres_choix_gauche $ch) -}}
          {{- warnf "____[ %s ]         : PAGE map value     : %s = %v" $ch $ch (index $.Page.Params.lettres_choix_gauche $ch) -}}
          {{- warnf "____[ %s ]         : PAGE then SITE val : %s = %v" $ch $ch ($.Page.Param (add "lettres_choix_gauche." $ch)) -}}
          {{- warnf "____[ %s ]         : === END ============" $ch }}
...
play with the chars and maps
WARN  DEST is R: R -> abvfä
WARN  [ [a b v f ä] ]
WARN  ____[ a ]         : === BEGIN =
WARN  ____[ b ]         : === BEGIN =
WARN  ____[ b ]         : SITE map defined?  : true
WARN  ____[ b ]         : SITE map content   : map[b:true space:true v:true]
WARN  ____[ b ]         : PAGE map defined?  : true
WARN  ____[ b ]         : PAGE map content   : map[v:false]
WARN  ____[ b ]         : --------------------
WARN  ____[ b ]         : SITE map value     : b = true
WARN  ____[ b ]         : PAGE map value     : b = <nil>
WARN  ____[ b ]         : PAGE then SITE val : b = true
WARN  ____[ b ]         : === END ============
WARN  ____[ v ]         : === BEGIN =
WARN  ____[ v ]         : SITE map defined?  : true
WARN  ____[ v ]         : SITE map content   : map[b:true space:true v:true]
WARN  ____[ v ]         : PAGE map defined?  : true
WARN  ____[ v ]         : PAGE map content   : map[v:false]
WARN  ____[ v ]         : --------------------
WARN  ____[ v ]         : SITE map value     : v = true
WARN  ____[ v ]         : PAGE map value     : v = false
WARN  ____[ v ]         : PAGE then SITE val : v = false
WARN  ____[ v ]         : === END ============
WARN  ____[ f ]         : === BEGIN =
WARN  ____[ ä ]         : === BEGIN =
WARN  ____[ ä ]         : SITE map defined?  : true
WARN  ____[ ä ]         : SITE map content   : map[b:true space:true v:true]
WARN  ____[ ä ]         : PAGE map defined?  : true
WARN  ____[ ä ]         : PAGE map content   : map[v:false]
WARN  ____[ ä ]         : --------------------
WARN  ____[ ä ]         : SITE map value     : ä = <nil>
WARN  ____[ ä ]         : PAGE map value     : ä = <nil>
WARN  ____[ ä ]         : PAGE then SITE val : ä = <nil>
WARN  ____[ ä ]         : === END ============
ERROR File C:\_repos\github\clone\topic-56590-multilang-param\hugo-sample\content\_index.md: char "ä" in text "abvfä" is not in left/right lists

I meant list_left and list_right in the configuration. Sorry I didn’t have the file under my eyes. I just removed everything from the repo not related to our issue. It should be simpler to reason.
I can not the good ol’ “default”, because I wanted the configurations to merge, aka that only the key mentioned in the frontmatter be updated.

Right now this

{{ if eq .Destination "R"}}
{{- $chars := split (htmlUnescape .Text) "" -}}
{{- range $chars  }}{{ . }}{{ $ch := . }}
{{- if strings.Contains site.Params.list_left . }} Left hand
	{{- else if strings.Contains site.Params.list_right . }} Right hand
	{{- else }}{{ with site.Param "lettres_choix_gauche" -}}
		{{- if isset . $ch }}
			{{- if index . $ch }} Left hand{{ else }} Right{{ end }}
		{{- else }}{{- errorf "SCREAM %s" $.Text -}}
		{{- end }}
	{{- end }}
{{- end }}
{{  end }}
{{- end }}
Map lettres_choix_gauche:
{{ site.Param "lettres_choix_gauche" }}

access the config map just fine. But i can’t update or merge anything.

you cannot change or update a frontmatter parameter.

what do you want to do?

No I don’t mean to change it. I want keys defined in the frontmatter to override those in the config selectively. Is it possible ?

I feel like trying to escape a twister - We can implement something that prefers a a frontmatter defined parameter instead of the one set in site config. that has been shown in the code and descriptions above.

Map lettres_choix_gauche:
{{ site.Param "lettres_choix_gauche" }}

This will report the site parameter as defined in the config - so that’s expected you cannot change these map. We could build a new map to hold values you want …

BUT I would recommend to step out of your code box and switch to let’s say Use case or Requirements.

Something like: (keep in mind that this is just my understanding so the content may not match your requirements)

I have defined a default set of keys that should be typed with the left hand, and another set of keys that should be typed with the right hand by default.

The list is not complete it holds only the basic sets. that should always be typed as defined.

For any key NOT in these default lists I want …

I guess I tried too much at once. If there is no command to merge the frontmatter and config maps, it suffices to do it ourselves and it wortks:

Page.Params.lettres_choix_gauche: {{ $.Page.Params.lettres_choix_gauche }}
{{ if eq .Destination "R"}}
{{- $chars := split (htmlUnescape .Text) "" -}}
{{- range $chars  }}{{ . }}{{ $ch := . }}
{{- if strings.Contains site.Params.list_left . }} Left hand
	{{- else if strings.Contains site.Params.list_right . }} Right hand
	{{- else -}}
		{{ $merged := merge site.Params.lettres_choix_gauche $.Page.Params.lettres_choix_gauche }}
		{{- if isset $merged $ch -}}
			{{- if index $merged $ch }} Left hand{{ else }} Right{{ end -}}
		{{- else }}{{- errorf "SCREAM %s" $.Text -}}
		{{- end }}
	{{- end }}
{{  end }}
{{- end }}
Map .Page.Params.lettres_choix_gauche:
{{ site.Param "lettres_choix_gauche" }}

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