Using hierarchical markdown files to create javascript structure

I want to pass hierarchical directory template data to javascript. I want to build an array from header data of child.md files from sub-directories. One row in the array for each sub-directory.

I’m able to create the variables and append data in the parent.html.twig file, but I cannot append the records using a child.md template.

Is there some way to arrange this so that I can append the data from child templates into a variable in the parent template?

The templates I’ve tried.

In parent template:

{% block content %}
{% set portfolio_groups = [] %}
{% set portfolio_model = '/user/pages/models/' ~ page.header.model %}
{% for module in page.collection() %}
  {% if module.template %}
    {% include module.template ~ '.html.twig' %}
  {% endif %}
{% endfor %}
<div id="wrapper">
<div id="layer-gl"></div>
<div id="layer-css"></div>
{% if page.header.model %}
{% set portfolio = { model:portfolio_model, groups:portfolio_groups } %}
{% do assets.addInlineJs('const portfolio = ' ~ portfolio|json_encode ~ ';', { group: 'bottom' }) %}
{% endif %}
<script type="importmap">
{
        "imports": {
                "three": "{{ theme_url }}/js/src/Three.js",
                "three/addons/": "{{ theme_url }}/js/jsm/"
        }
}
</script>
<script type="module" src="{{ theme_url }}/js/portfolio.js"></script>
</div>
{% endblock %}

In child template:

<!-- {{ module.header.model }} -->
{% set portfolio_groups = portfolio_groups|merge([{ model:module.header.model, items: [] }]) %}
{{ parent }}

I found a solution to this that works, but if someone has a better way, please chime in.

I created a plugin that accesses a php global variable, I just need to be sure to bin/grav clearcache after changes.