Skip to content

Instantly share code, notes, and snippets.

@N-Parsons
Created May 15, 2019 11:30
Show Gist options
  • Save N-Parsons/faa3d85a64118c1e4a1f0125e2a56e87 to your computer and use it in GitHub Desktop.
Save N-Parsons/faa3d85a64118c1e4a1f0125e2a56e87 to your computer and use it in GitHub Desktop.
Grav plugin/function to determine whether there is an explicit summary in the page content. This allows you to safely slice the summary off using "{{ content|slice(page.summary|length)|raw }}" if "page.header.has_explicit_summary" is true.
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use \RocketTheme\Toolbox\Event\Event;
class SummaryExplicitnessPlugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
'onPageContentRaw' => ['setSummaryExplicitness', 0],
];
}
/**
* Check whether the page has an explicit summary
*
* Based on: https://discourse.getgrav.org/t/page-with-or-without-summary/2611/12
*/
public function setSummaryExplicitness(Event $event)
{
// Get the current raw content
$page = $event['page'];
$raw = $page->getRawContent();
$summaryseparator = $this->config->get("site.summary.delimiter");
$summaryseparatorpos = mb_strpos($raw, $summaryseparator);
if ($summaryseparatorpos) {
$page->header()->has_explicit_summary = true;
}
else {
$page->header()->has_explicit_summary = false;
}
}
}
@N-Parsons
Copy link
Author

I've reformatted this as a plugin to share here, but I actually include the function within my theme, since the theme relies on it.

To utilise this in a page template, use something like:

{% if page.header.has_explicit_summary %}
  {{ content|slice(page.summary|length)|raw }}
{% else %}
  {{ content|raw }}
{% endif %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment