Objective:
I want to render a Shopify section
inside of a template
using a variable as the section file's name. i.e.
Example
Template: templates/parent.liquid
Section: sections/child.liquid
Attempting to render (include) child.liquid
inside parent.liquid
when variableName = 'child'
context: *child*
could be anything -- in my theme's specific use case, I'm assigning variableName = page.handle
. If page.handle
matches an existing product vendor AND sections/[page.handle].liquid
exists, I am includuing it in the page.
WHY? I want to avoid a hardcoded list of available section-files in lieu if this proposed progammatic approach of dynamic filenames derived from the current page.
The following code would go inside templates/parent.liquid
this works (hardcoded quoted strings):
BUT, I want to avoid hard-coded filenames at all costs.
{% section 'child' %}
these all throw an error (any form of dynamic filename):
{% section variableName %}
{% section {{ variableName }} %}
{% section 'variableName' %}
{% section "'" + variableName + "'" %}
{% section variableName %}
{% section "{{ variableName }}" %}
{% section [variableName] %}
edit more errors
{% render variableName %}
Syntax error in tag 'render' - Template name must be a quoted string
{% include variableName %}
look for a snippet, not a section
{% include sections.variableName %}
Liquid error: Argument error in tag 'include' - Illegal template name
{% include sections.[variableName] %}
Liquid error: Argument error in tag 'include' - Illegal template name
Research The closest article I've found via Google is https://community.shopify.com/c/Shopify-Design/Dynamic-Liquid-Variable-inside-Liquid-tag/td-p/162451
Pseudo Answer I'm searching for a working solution along the lines of:
{% include sections.[page.handle] %}