Browse Source

Issue #3087368: Add format_size Twig filter

merge-requests/4/head
Chi 5 years ago
parent
commit
cf7ec3f5d7
  1. 7
      src/TwigExtension.php
  2. 4
      tests/src/Functional/TwigTweakTest.php
  3. 1
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

7
src/TwigExtension.php

@ -293,6 +293,13 @@ class TwigExtension extends \Twig_Extension {
// @endcode
new \Twig_SimpleFilter('check_markup', [$this, 'checkMarkup']),
// - Format Size -
//
// @code
// {{ 12345|format_size() }}
// @endcode
new \Twig_SimpleFilter('format_size', 'format_size'),
// - Truncate -
//
// @code

4
tests/src/Functional/TwigTweakTest.php

@ -320,6 +320,10 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-check-markup"]';
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());
// -- Test truncation.
$xpath = '//div[@class = "tt-truncate" and text() = "Hello…"]';
$this->assertByXpath($xpath);

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

@ -58,6 +58,7 @@
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</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-format-size">{{ 12345|format_size() }}</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-nested">{{ {alpha: {beta: {gamma: 123}}}|with(['alpha', 'beta', 'gamma'], 456)|json_encode|replace({'"':''}) }}</div>

Loading…
Cancel
Save