Dynamically construct url param

Hi,

Am trying to dynamically construct url param from local variable.

config.yaml
    params:
         share: 
             url: "example/shareArticle"
             urlParam:
                 url: pageUrl
                 summary: body

partials.html

{{ $title := .Title }}
{{ $pageUrl := .Permalink }}
{{ $body := print $title " by MyDeveloperPal. " $pageUrl }}

  {{with .Site.Params.share}}
        <div onclick="window.open('{{- .url -}}?{{- range $index, $element := .urlParam -}}{{- $index -}}={{- $element -}}&{{ end }}');">
            share Content
        </div>
   {{end}} 

Currently this generates

example/shareArticle?url=pageUrl&summary=body&

But I want to dynamically generates based on the local variables in partials i.e.

example/shareArticle?url=https://myblog.com/article&summary=Article by MyDeveloperPal https://myblog.com/article&

Appreciate your response.

As per your quoted template, it appears that you can simply add your $body variable to the above, to render your desired output.

i.e.

onclick="window.open('{{- .url -}}?{{- range $index, $element := .urlParam -}}{{- $index -}}={{- $element -}}&{{ end }}{{ $body }}`);"

Nope. Thank you @onedrawingperday for your answer.
Let me rephrase my question.

In config, I have the following map for urlParam
url: pageUrl
summary: body

I am trying to iterate the map and generate queryParams where

  • key in the queryParam matches the key in the map
  • value in the queryParam is populated by the local variable whose name is identified by the value in the map.

For example,
?url={{ load the local variable pageUrl}}&summary={{ load the local variable body}}&

Hi there.

Not sure I fully understand your case, but if each page has it’s own .Permalink and .Description (or custom equivalent), no need to use Site Params for that. Front matters for the given page should work.

Or maybe you want to variabilize the front matter variables as well ? Not sure that’s feasable.