Introduce yourself here! In order to avoid spam, new users must first comment on the discussion in the 'Introductions' before they become 'full members'
I rewrote some Smarty filters
  • I know this isn’t a Smarty forum but Smarty is an essential part of Flatpress. I was thinking about how to display the size of documents in the Static Page List and discovered variable modifiers in Smarty. So I added a bit to admin\panels\static\admin.static.list.tpl:
    <td>{$content|count_words}<br>{$content|count_characters}</td>
    with a happy smile. It didn’t work. Neither count_words nor count_characters work with long files, and having a two megabytes static page results in an allocation error in either modifier. So I rewrote both. (Top remarks omitted here, I didn’t change them.)

    fp-includes\smarty\plugins\modifier.count_words.php:

    function smarty_modifier_count_words($string)
    {
    /*
        // split text by ' ',\r,\n,\f,\t
        $split_array = preg_split('/\s+/',$string);
        // count matches that contain alphanumerics
        $word_count = preg_grep('/[a-zA-Z0-9\\x80-\\xff]/', $split_array);

        return count($word_count);
    */

    // Long file aware version by LAttilaD

    $string=str_replace(' ', ' ', $string, $spaces);
        $string=str_replace("\n", "\n", $string, $enters);
        return $spaces+$enters;
    }

    I know this version isn’t as accurate as the old one, but it won’t stop at a longer file.

    fp-includes\smarty\plugins\modifier.count_characters.php:

    function smarty_modifier_count_characters($string, $include_spaces = false)
    {
        if ($include_spaces)
           return(strlen($string));

    //    return preg_match_all("/[^\s]/",$string, $match);

    // Long file aware version by LAttilaD
    $string=str_replace(' ', ' ', $string, $spaces);
        return strlen($string)-$spaces;

    }

  • PS. To display document lengths with thousand separators, use
    {$content|count_words|regex_replace:"/(?<=[0-9])(?=(?:[0-9][0-9][0-9])+(?![0-9]))/":"$0,"}
    (or the same with characters) in your template. The comma is the separator, change it to any character you need. Note that I had to write [0-9] three times because Smarty doesn’t accept {3} as regex pattern (it tries to translate it to a variable).
  • smarty supports every php function. You could use |str_word_counts and |strlen

    (Also, I've changed the title of this discussion)
Start a New Discussion

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion