Anyway to dynamically have values based in the .md params?

I want to be able to build out a form field from function Params from my .md files.

Kind of like shopify variants, is this possible to do? Say a page has a value like:

Sizes: [small, med, large] how can I generate something like this?

<select name="size" id="size">
    <option value="small">Small</option>
    <option value="med">Med</option>
    <option value="large">Large</option>
</select>

I envision something like this:
<select name="{{ .Params.option.size }}"...

something like this,

<select name="size" id="size">

{{ range $size := .Params.option.size }}
  <option value="{{ $size }}">{{ $size | humanize }}</option>
{{ end }}

</select>
2 Likes

Thanks @pamubay but that’s not dynamic in the sense that I have to use the .options.size as a static key. Is there any way to make that so it’s grabbing the value then applying it?

The only way is to template out a bunch of conditions and check to see if it exists.

You can range on the .Params, but it’s not really good idea because you need to filter out the unrelated field.

my idea is put it under a one key here e.g. option

---
option:
  sizes: ["small","med","large"]
  color: ["red","blue","black"]
---
{{ range $variantName, $value := .Params.option }}
{{ if len $value }} <!-- check if value(array) is not empty -->

<select name="{{ $variantName }}" id="{{ $variantName }}">
  {{ range $v := $value }}
    <option value="{{ $v }}">{{ $v | humanize }}</option>
  {{ end }}
</select>

{{ end }}
{{ end }}
2 Likes

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