◀ All posts

Truncate Text In Lektor

Truncate Text In Lektor
moss ramp · hover for pal() swap · full-colour image

In this example, variable post is an instance of Markdown. Snippet was tested with Jinja3 template engine. I use it for generating my blog entry previews.

{{ post.body.html | striptags | truncate(length=200, killwords=False, end='...') }}
  • post.body.html: is the Lektor object containing regular string or HTML text.
  • striptags: Removes all HTML markup and returns pure text. docs
  • truncate(length=200, killwords=False, end='...'): truncate text to 200 symbols, stop at the nearest word that fits this length, and add “…” in the end of the truncated string. docs