Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Archive

Using truncate() in templates

Started by Muut Archive 11 years ago · 7 replies · 1297 views
11 years ago

Looks like we neglected to add that to the docs. Will get to it soon. If you look at the code in the TwigExtension.php class you will see:

PHP
    /**
     * Truncate content by a limit.
     *
     * @param  string $string
     * @param  int    $limit    Max number of characters.
     * @param  string $break    Break point.
     * @param  string $pad      Appended padding to the end of the string.
     * @return string
     */
    public function truncateFilter($string, $limit = 150, $break = ".", $pad = "…")
    {
        // return with no change if string is shorter than $limit
        if (strlen($string) <= $limit) {
            return $string;
        }

        // is $break present between $limit and the end of the string? 
        if (false !== ($breakpoint = strpos($string, $break, $limit))) {
            if ($breakpoint < strlen($string) - 1) {
                $string = substr($string, 0, $breakpoint) . $pad;
            }
        }

        return $string;
    }

Basically it limits a string to a configurable number of characters and appends a pad (...) to the end. Note however this doesn't strip tags, so that might be causing your inconsistencies. You can pass your content through striptags filter first to sort this out though:

TWIG
{{ content|striptags|truncate(200) }}
11 years ago

Thanks. Ok for the documentation part but what about the problem I mentioned ?

11 years ago

Did you not read the stiptags comment I made? You really didn't provide any specifics about your problem.

11 years ago

Yes, I noted about the striptags but I still have this weird result with inconsistent length

My code :

TWIG
{{ portfolio.desc|striptags|truncate(50) }}

and the result :

http://imgur.com/IV3h5zD

137 chars for 1st block, 200 for second, 120 for third .....

11 years ago

Ok I looked and I found the code was actually smarter than I remember. The logic was such that it would go to the end of the sentence given any character count. This way, your truncated text is not cut off, it will go to the nearest end of the sentence.

However, I can appreciate this is probably not what is wanted all the time so in the next release of Grav, this logic will be turned off by default, but you can still use it if you pass a second param of true, so:

TWIG
{{ 'one sentence. two sentences'|truncate(5) }}

Will print: one s...

TWIG
{{ 'one sentence. two sentences'|truncate(5, true) }}

Will print: one sentence.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1318 9 years ago
Archive · by Muut Archive, 9 years ago
2 915 9 years ago
Archive · by Muut Archive, 9 years ago
2 4044 9 years ago
Archive · by Muut Archive, 9 years ago
1 2920 9 years ago
Archive · by Muut Archive, 9 years ago
3 1103 9 years ago