Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.