Skip to content

Instantly share code, notes, and snippets.

@piotrpog
Last active July 11, 2019 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrpog/3349bbf4ff2018433172f00ae0f9032f to your computer and use it in GitHub Desktop.
Save piotrpog/3349bbf4ff2018433172f00ae0f9032f to your computer and use it in GitHub Desktop.
Macro truncating text by the number of characters. More info: http://craftsnippets.com/articles/truncating-text-with-twig-macros-in-craft-cms
{# v1 #}
{% macro truncateChars(text, limit, suffix) %}
{% spaceless %}
{# settings #}
{% set defaultSuffix = '...' %}
{# logic #}
{% if text and limit %}
{% set text = text|striptags %}
{% set suffix = suffix|default(defaultSuffix) %}
{% set stringy = create(
"Stringy\\Stringy",
[text]
) %}
{{ stringy.safeTruncate(limit, suffix) }}
{% endif %}
{% endspaceless %}
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment