Browse Source

Issue #3087368 Add 'format_size' filter

merge-requests/2/head
Chi 4 years ago
parent
commit
a1bd01c6df
  1. 5
      README.md
  2. 1
      src/TwigTweakExtension.php
  3. 4
      tests/src/Functional/TwigTweakTest.php
  4. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

5
README.md

@ -228,6 +228,11 @@ Twig filters.
{{ '<b>bold</b> <strong>strong</strong>'|check_markup('restricted_html') }} {{ '<b>bold</b> <strong>strong</strong>'|check_markup('restricted_html') }}
``` ```
### Format size
```twig
{{ 12345|format_size() }}
```
### Truncate ### Truncate
```twig ```twig
{# Truncates a UTF-8-encoded string safely to 10 characters. #} {# Truncates a UTF-8-encoded string safely to 10 characters. #}

1
src/TwigTweakExtension.php

@ -80,6 +80,7 @@ class TwigTweakExtension extends AbstractExtension {
new TwigFilter('image_style', [self::class, 'imageStyleFilter']), new TwigFilter('image_style', [self::class, 'imageStyleFilter']),
new TwigFilter('transliterate', [self::class, 'transliterateFilter']), new TwigFilter('transliterate', [self::class, 'transliterateFilter']),
new TwigFilter('check_markup', 'check_markup'), new TwigFilter('check_markup', 'check_markup'),
new TwigFilter('format_size', 'format_size'),
new TwigFilter('truncate', [Unicode::class, 'truncate']), new TwigFilter('truncate', [Unicode::class, 'truncate']),
new TwigFilter('view', [self::class, 'viewFilter']), new TwigFilter('view', [self::class, 'viewFilter']),
new TwigFilter('with', [self::class, 'withFilter']), new TwigFilter('with', [self::class, 'withFilter']),

4
tests/src/Functional/TwigTweakTest.php

@ -310,6 +310,10 @@ final class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-check-markup"]'; $xpath = '//div[@class = "tt-check-markup"]';
self::assertEquals('<b>bold</b> strong', trim($this->xpath($xpath)[0]->getHtml())); self::assertEquals('<b>bold</b> 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. // -- Truncate.
$xpath = '//div[@class = "tt-truncate" and text() = "Hello…"]'; $xpath = '//div[@class = "tt-truncate" and text() = "Hello…"]';
$this->assertXpath($xpath); $this->assertXpath($xpath);

1
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -64,6 +64,7 @@
<div class="tt-image-style">{{ 'public://images/ocean.jpg'|image_style('thumbnail') }}</div> <div class="tt-image-style">{{ 'public://images/ocean.jpg'|image_style('thumbnail') }}</div>
<div class="tt-transliterate">{{ 'Привет!'|transliterate('ru') }}</div> <div class="tt-transliterate">{{ 'Привет!'|transliterate('ru') }}</div>
<div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>'|check_markup('twig_tweak_test') }}</div> <div class="tt-check-markup">{{ '<b>bold</b> <strong>strong</strong>'|check_markup('twig_tweak_test') }}</div>
<div class="tt-format-size">{{ 12345|format_size() }}</div>
<div class="tt-truncate">{{ 'Hello world!'|truncate(10, true, true) }}</div> <div class="tt-truncate">{{ 'Hello world!'|truncate(10, true, true) }}</div>
<div class="tt-with">{{ {'#markup':'Example'}|with('#prefix', '<b>')|with('#suffix', '</b>') }}</div> <div class="tt-with">{{ {'#markup':'Example'}|with('#prefix', '<b>')|with('#suffix', '</b>') }}</div>
<div class="tt-with-nested">{{ {alpha: {beta: {gamma: 123}}}|with(['alpha', 'beta', 'gamma'], 456)|json_encode|replace({'"':''}) }}</div> <div class="tt-with-nested">{{ {alpha: {beta: {gamma: 123}}}|with(['alpha', 'beta', 'gamma'], 456)|json_encode|replace({'"':''}) }}</div>

Loading…
Cancel
Save