Breadcrumb

Here is my improved version to build breadcrumbs

# usage
{{ macros::breadcrumb(path=["/tags", term.path], titles=["Tags", term.name]) }}
{{ macros::breadcrumb_page(path=section.components, titles=section.components) }}

# definition
{% macro breadcrumb(path, titles) %}
<script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "BreadcrumbList",
        "itemListElement":
        [
            {
                "@type": "ListItem",
                "position": 1,
                "item": {
                    "@id": "{{ get_url(path="", trailing_slash=false) }}",
                    "name": "{{ config.title | safe }}"
                }
            }{% if path | length > 0 %},{% endif %}
            {%- for crumb in path %}
            {
                "@type": "ListItem",
                "position": {{ loop.index + 1 }},
                "item": {
                    "@id": "{{ get_url(path=crumb, trailing_slash=false) }}",
                    "name": "{{ titles[loop.index0] | safe }}"
                }
            }{% if not loop.last %},{% endif %}
            {%- endfor %}
        ]
    }
</script>

{%- endmacro %}
{% macro breadcrumb_page(path, titles) %}
{% set cur = "/" %}
{% set path_bc = [] %}
{% for elt in path %}
    {% set_global cur = cur ~ elt ~ "/" %}
    {% set_global path_bc = path_bc | concat(with=cur) %}
{% endfor %}
{{- jsonld_macros::breadcrumb(path=path_bc, titles=titles) -}}
{%- endmacro %}

Source: blog.williamdes.eu/jsonld_macros.html at a7ac3b592fecc9c43640a6be9e376f379471f95a · wdesportes/blog.williamdes.eu · GitHub

1 Like