diff --git a/README.md b/README.md index ace749b..baa5b51 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,11 @@ Twig filters. {{ 'bold strong'|check_markup('restricted_html') }} ``` +### Format size +```twig +{{ 12345|format_size() }} +``` + ### Truncate ```twig {# Truncates a UTF-8-encoded string safely to 10 characters. #} diff --git a/src/TwigTweakExtension.php b/src/TwigTweakExtension.php index 404a3b0..c298446 100644 --- a/src/TwigTweakExtension.php +++ b/src/TwigTweakExtension.php @@ -80,6 +80,7 @@ class TwigTweakExtension extends AbstractExtension { new TwigFilter('image_style', [self::class, 'imageStyleFilter']), new TwigFilter('transliterate', [self::class, 'transliterateFilter']), new TwigFilter('check_markup', 'check_markup'), + new TwigFilter('format_size', 'format_size'), new TwigFilter('truncate', [Unicode::class, 'truncate']), new TwigFilter('view', [self::class, 'viewFilter']), new TwigFilter('with', [self::class, 'withFilter']), diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index e85ea68..a23fe6e 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -310,6 +310,10 @@ final class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-check-markup"]'; self::assertEquals('bold strong', trim($this->xpath($xpath)[0]->getHtml())); + // -- Format size. + $xpath = '//div[@class = "tt-format-size"]'; + self::assertSame('12.06 KB', $this->xpath($xpath)[0]->getHtml()); + // -- Truncate. $xpath = '//div[@class = "tt-truncate" and text() = "Hello…"]'; $this->assertXpath($xpath); diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index 99201a1..dcc9f28 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -64,6 +64,7 @@