Nested Shortcodes - Get Furthest/Farthest Parent Param

Created a partial to act as a function, finding a nested shortcode’s farthest parent parameter. Sets it to scratch for use in template following shortcode call. Works with multiple calls to the same shortcode for a given page.

Our team has been using nested shortcodes to build modular website or prototype websites. We really like the flexibility of arrangement of shortcodes helps when the content markup needs to be complex and using a page template is too rigid. Sometimes we want things to adapt based on their context (parent shortcode) or say we need a title from a parent shortcode to populate an id or relationship in the DOM.

Link to the inverse, Nested Shortcodes, Closest Parent Param

We created this small partial that uses a recursive call (like menu partial) to find the outer most parent shortcode parameter and then work it’s way back to the original. Hopefully this is useful to someone out there. Instructions for use are in comments near the top the file.

— START PARTIAL FILE —

{{/* 

  The following partial/function looks for the furthest parent parameter in nested shortcodes. It will also check itself. It then retrieves
  it and sets it to a scratch variable with the same name as the file name minus "return--". That variable is then populated 
  and available after the call to the partial.

  EXAMPLE SHORTCODE STRUCTURE:
  {{< parent title="Hello World" >}}
    {{< subParent >}}
      {{< child >}}
    {{< /subParent >}}
  {{< /parent >}}

  USAGE (inside child shortcode):
  {{ $var := $.Page.Scratch }}
  {{ partial "return--parentParamFurthest.html" (dict "current" . "parameterKey" "title") }}
  {{ $parentTitle := $var.Get "parentParamFurthest" }}

  PARAMETERS:           dict        (current, parameterKey) 
  - current             context     current shortcode scope/context (ie ".")
  - parameterKey        string      Shortcode parameter key

  RETURNS: 
  - False if no parameter is found

*/}}

{{ $var := .current.Page.Scratch }}
{{ $var.Set "parentParamFurthest" false }}
{{ $var.Set "parentParameterFound" false }}
{{ $param := .current.Get .parameterKey }}
<!-- First Checking if shortcode has a parent -->
{{ if .current.Parent }}
  <!-- If parent go to parent  -->
  {{ partial "fn/return--parentParamFurthest.html" (dict "current" .current.Parent "parameterKey" .parameterKey) }}
  <!-- After checking the parent continue on and check this one -->
  {{ if and $param (ne ($var.Get "parentParameterFound") true )}}
    {{ $var.Set "parentParamFurthest" $param }}
    {{ $var.Set "parentParameterFound" true }}
  {{ end }}
<!-- LAST PARENT FOUND (Or first if no parent) If they did not have a parent (last parent) check the items parameters -->
{{ else }}
  {{ if and $param (ne ($var.Get "parentParameterFound") true )}}
    {{ $var.Set "parentParamFurthest" $param }}
    {{ $var.Set "parentParameterFound" true }}
  {{ end }}
{{ end }}
1 Like

There was a mistake in paths based on my own folder structure, directory of internal partial --> layouts/partials/fn/ , which is now “layout/partials/”.

Update below:

{{/* 

  The following partial/function looks for the furthest parent parameter in nested shortcodes. It will also check itself. It then retrieves
  it and sets it to a scratch variable with the same name as the file name minus "return--". That variable is then populated 
  and available after the call to the partial.

  EXAMPLE SHORTCODE STRUCTURE:
  {{< parent title="Hello World" >}}
    {{< subParent >}}
      {{< child >}}
    {{< /subParent >}}
  {{< /parent >}}

  USAGE (inside child shortcode):
  {{ $var := $.Page.Scratch }}
  {{ partial "return--parentParamFurthest.html" (dict "current" . "parameterKey" "title") }}
  {{ $parentTitle := $var.Get "parentParamFurthest" }}

  PARAMETERS:           dict        (current, parameterKey) 
  - current             context     current shortcode scope/context (ie ".")
  - parameterKey        string      Shortcode parameter key

  RETURNS: 
  - False if no parameter is found

*/}}

{{ $var := .current.Page.Scratch }}
{{ $var.Set "parentParamFurthest" false }}
{{ $var.Set "parentParameterFound" false }}
{{ $param := .current.Get .parameterKey }}
<!-- First Checking if shortcode has a parent -->
{{ if .current.Parent }}
  <!-- If parent go to parent  -->
  {{ partial "return--parentParamFurthest.html" (dict "current" .current.Parent "parameterKey" .parameterKey) }}
  <!-- After checking the parent continue on and check this one -->
  {{ if and $param (ne ($var.Get "parentParameterFound") true )}}
    {{ $var.Set "parentParamFurthest" $param }}
    {{ $var.Set "parentParameterFound" true }}
  {{ end }}
<!-- LAST PARENT FOUND (Or first if no parent) If they did not have a parent (last parent) check the items parameters -->
{{ else }}
  {{ if and $param (ne ($var.Get "parentParameterFound") true )}}
    {{ $var.Set "parentParamFurthest" $param }}
    {{ $var.Set "parentParameterFound" true }}
  {{ end }}
{{ end }}