Untitled

count matches
 avatar
tommydalton
twig
2 months ago
705 B
4
Indexable
Never
{% set query = props.query %}
{% set description = entry.seo.metaGlobalVars.parsedValue('seoDescription') ?? '' %}

{# Split the description by the query to count occurrences #}
{% set occurrences = description|split(query) %}

{# Subtract 1 from the total split parts to get the match count #}
{% set matchedCount = (occurrences|length > 0) ? occurrences|length - 1 : 0 %}

{# Check if there's at least one match #}
{% if matchedCount > 0 %}
    {# Set the matched content #}
    {% set matchedContent = query %}

    {# Output the count and the HTML with the matched content #}
    <div>Displaying {{ matchedCount }}</div>
    <div>{{ entry.id }}: {{ matchedContent }}</div>
{% endif %}
Leave a Comment