1tomany/twig-filesize

A Twig extension to render human readable filesizes

v0.0.1 2025-05-15 22:28 UTC

This package is auto-updated.

Last update: 2025-05-15 22:33:11 UTC


README

This Twig extension allows you to easily convert an integer, in bytes, to a human readable string with the correct unit.

Installation

Install the library with Composer:

composer require 1tomany/twig-filesize

Tag the OneToMany\Twig\FilesizeExtension as a Twig extension in your services configuration:

services:
    OneToMany\Twig\MailtoExtension:
        tags: [twig.extension]

Usage

This extension exposes a single filter named filesize with the following arguments:

  • scale: The number of decimals to display. The scale must be between 0 and 4, inclusive, and has a default value of 0.
  • spacer: A string that separates the filesize and unit. The spacer is an empty string by default.
{{ 851856|filesize(1) # 831.9KB }}
{{ 851856|filesize(0, ' ') # 832 KB }}

Testing

./vendor/bin/phpunit

Credits

License

The MIT License