Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

12. Dynamic Summary Generation

Andrew Welch edited this page Apr 18, 2019 · 2 revisions

No Maintenance Intended

DEPRECATED

This Craft CMS 2.x plugin is no longer supported, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons.

The Craft CMS 3.x version of this plugin can be found here: craft-seomatic and can also be installed via the Craft Plugin Store in the Craft CP.

Dynamic Summary Generation

Generating a good summary from dynamic data is also a pain; to this end, SEOmatic uses the TextRank PHP library to generate a summary from arbitrary text data.

All three of these methods accomplish the same thing:

{# Extract a summary using the 'extractSummary' function #}
{{ extractSummary( TEXT, LIMIT ) }}

{# Extract a summary using the 'extractSummary' filter #}
{{ TEXT | extractSummary( LIMIT ) }}

{# Extract a summary using the 'extractSummary' variable #}
{% do craft.seomatic.extractSummary( TEXT, LIMIT ) %}

TEXT is the text to extract the summary from, and the optional LIMIT parameter specifies the maximum number of characters to return. The Summary is returns is at most 5% of the sentences of the text.

Caveats - This feature of TextRank seems to be best suited for large amounts of text. It attempts to pick out the most relevant whole sentences based on statistical analysis. The result may end up being too long to be useful for an seoDescription in some cases.

So tying it all together, you might do something like this for a dynamic Blog entry:

{% set seomaticMeta = { 
    seoTitle: entry.title,
    seoDescription: extractSummary(entry.blog),
    seoKeywords: extractKeywords(entry.blog),
    seoImage: entry.image.url,
    canonicalUrl: seomaticMeta.canonicalUrl,
    twitter: { 
        card: seomaticMeta.twitter.card,
        site: seomaticMeta.twitter.site,
        creator: seomaticMeta.twitter.creator,
        title: entry.title,
        description: extractSummary(entry.blog),
        image: entry.image.url
    },
    og: { 
        type: seomaticMeta.og.type,
        locale: seomaticMeta.og.locale,
        url: entry.url,
        title: entry.title,
        description: extractSummary(entry.blog),
        image: entry.image.url,
        site_name: seomaticMeta.og.site_name,
        see_also: seomaticMeta.og.see_also
    }
} %}

Note that we set the canonicalUrl to seomaticMeta.canonicalUrl, effectively leaving it unchanged.

Anywhere we are setting a field to seomaticMeta.*, we're setting it to what it already is, essentially saying to leave it unchanged. We do this because Twig requires that you pass in the entire array to the set operator.