Last active
July 11, 2019 19:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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