Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Using hierarchical markdown files to create javascript structure

theme

Solved by Steven Green View solution

Started by Steven Green 1 year ago · 2 replies · 73 views
1 year ago

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?

1 year ago

The templates I've tried.

In parent template:

TWIG
{% 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:

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

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.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 99 16 hours ago
General · by pamtbaau, 21 hours ago
1 63 21 hours ago
General · by Andy Miller, 1 day ago
0 48 1 day ago
General · by Marcel, 12 months ago
6 357 5 days ago
General · by Duc , 6 days ago
3 45 6 days ago