{excerpt}
Cuts a long text to whole words with an ellipsis, centred on the first match of search words, every match marked.
Syntax
{excerpt $text, words=40}
{excerpt $text, words=24, highlight=$q, ellipsis='...'}
{excerpt $html, words=30, html}
Parameters
-
text - The text to cut.
Options
-
words - The length in words, default 40. A text no longer than that stays whole.
-
highlight -
Search words - a query, split at everything that is no letter or digit. The cut is centred on the first match and every match is put in a
<mark>. -
html -
Bare option: the text is HTML - its tags (and the content of
scriptandstyle) are dropped and its entities read before it is cut. -
ellipsis -
What stands for the text that was cut off, default
….
Description
{excerpt $body, words=24} writes the first 24 words of the text and an ellipsis where it goes on - always whole words, never a word cut in half. A text no longer than words stays whole, without an ellipsis.
With highlight= it is a search result: the query is split into words at everything that is no letter or digit, a word matches at the start of a word whatever its case - templ finds Templates - and the whole matched word is put in a <mark>. The window of words words is centred on the first match and moved in where it would run past either end, with an ellipsis on each side that was cut.
Everything of the text is escaped piece by piece; the only markup that comes out is the <mark> around a match. For a text that is HTML - a post body, a rendered page - the html option drops its tags and reads its entities first, so the words counted are the words a reader sees.
The mark colours are --pad-excerpt-mark and --pad-excerpt-marked on .pad-excerpt, with light-dark() defaults written once a page. A words that is no number of 1 or more is a strict error.
Examples
A search result
Twenty words around the first match of the query, every word that starts with a search word marked.
<p>Results for <strong>{$q}</strong>:</p>
<p>{excerpt $body, words=20, highlight=$q}</p>
<p>The first twenty words, no query: {excerpt $body, words=20}</p>
<?php
$q = 'cache ttl';
$body = 'Some parts of a page cost more to render than others: a list of the best selling '
. 'products, a menu built from the database, a report over a year of orders. The '
. 'fragment cache keeps the rendered text of such a section for a number of seconds and '
. 'serves it again on the next request without running the tags inside it. Name the '
. 'section, give it a ttl, and forget it when what it shows has changed.';
?>
Results for cache ttl:
… database, a report over a year of orders. The fragment cache keeps the rendered text of such a section for …The first twenty words, no query: Some parts of a page cost more to render than others: a list of the best selling products, a menu …
A teaser from HTML
html drops the tags and reads the entities before counting; ellipsis= sets the text for the rest.
<p>{excerpt $post, words=28, html, ellipsis='[more]'}</p>
<?php
$post = "<h2>Charts without JavaScript</h2>\n"
. "<p>A chart is a picture, and a picture can be <em>drawn on the server</em>. The chart tag "
. "writes inline SVG: bars, lines, pies & forty other kinds.</p>\n"
. "<script>track ( 'teaser' );</script>";
?>
Charts without JavaScript A chart is a picture, and a picture can be drawn on the server. The chart tag writes inline SVG: bars, lines, pies & forty [more]
See also
- {markdown} Renders its content, or the value of its parameter, as Markdown and writes the HTML.
- {diff} Shows what changed between two texts - word by word in the flow of the text, or line by line in one column or side by side.
- {lorem} Placeholder text - Lorem ipsum and Latin words - in words, sentences or paragraphs, the same text for the same seed.