tentwofour / twig
Twig extensions
Requires
- php: ^7.0|^8.0
- tentwofour/formatter: ^1.1
- tentwofour/inflector: ^1.1
- twig/intl-extra: ^2.0
- twig/twig: ^1.0|^2.0|^3.0
Requires (Dev)
- phpunit/phpunit: ^6.0
README
A few Twig extensions
EmailEncodingExtension
Provides a 'email_encode' filter, that encodes text into a random mix of ascii, hex, and plain characters. Pulled from the SF1 libraries.
email_encode(<string> text)
$ex = new EmailEncodingExtension() $ex->filter('f@example.com'); // => f@example.com
{{ 'f@example.com'|email_encode }} {# f@example.com #}
DiffExtension
Provides 2 functions, 'diff', and 'diff_html'.
diff(<iterable> a, <iterable> b)
Differentiates 2 arrays, and returns a multi-dimensional array containing the differences.
$ex = new DiffExtension(); // Will also work with multi-dimensional arrays $a = [ 'key' => 'value', ]; $b = [ 'key' => 'value_2', ]; $ex->diff($a, $b); // => [ // 0 => [ // 'd' => [ // 'key' => 'value', // ], // 'i' => [ // 'key' => 'value_2', // ], // ], // ]
{% set a = {'key': 'value'} %} {% set b = {'key': 'value_2'} %} {% set c = diff(a, b) %} {# { { 'd': { 'key': 'value' }, 'i': { 'key': 'value_2' } } } #}
diff_html(<string> a, <string> b, <string> output)
Differentiates 2 strings, and returns an array containing the differences, surrounded by HTML and tags.
Optionally returns a single string with the differences if output is set to 'string'.
$ex = new DiffExtension(); $old = 'That's what it said on "Ask Jeeves."'; $new = 'That's what it said on "Dogpile."'; // Output to array $ex->diffHtml($old, $new, 'array'); // => [ // 'old' => 'That's what it said on <del>"Ask Jeeves."</del>', // 'new' => 'That's what it said on <ins>"Dogpile."</ins>', // ] // Output to single string $ex->diffHtml($old, $new, 'string'); // => 'That's what it said on <del>"Ask Jeeves."</del><ins>"Dogpile."</ins>';
{{ diff_html('The lazy fox was eaten by the rabbid rabbit.', 'The stupid lazy fox was annihalited by the rabbit rabbit') }} {# 'The <ins>stupid</ins>lazy fox was <del>eaten</del><ins>annihilated</ins>by the <del>rabbid rabbit.</del><ins>rabbit rabbit</ins>' #}
InflectorExtension
Provides string inflection functions: 'camelcase_to_capitalized_words', 'camelcase_to_sentence_case_words', and 'camelcase_to_lower_case_words'.
$ex = new InflectorExtension(); $string = 'camelCaseWordWith1Number'; $ex->camelCaseToCapitalizedWords($string) // => 'Camel Case Word With 1 Number' $ex->camelCaseToSentenceCasedWords($string) // => 'Camel case word with 1 number' $ex->camelCaseToLowerCasedWords($string) // => 'camel case word with 1 number'
{{ 'camelCaseWordWith1Number'|camelcase_to_capitalized_words }} {# 'Camel Case Word With 1 Number' #} {{ 'camelCaseWordWith1Number'|camelcase_to_sentence_case_words}} {# 'Camel case word with 1 number' #} {{ 'camelCaseWordWith1Number'|camelcase_to_lower_case_words }} {# 'camel case word with 1 number' #}
MoneyExtension
Provides a 'cents_to_dollars' filter; which does exactly what it says it does.
$ex = new MoneyExtension(); $ex->centsToDollars(1200); // => '12.00'
{{ 1200|cents_to_dollars }} {# '12.00' #}
NumberExtension
Provides a 'number_to_human_readable' filter, to round to the closest thousand or million, and add a suffix (K, M).
number_to_human_readable(<int|float> number, $precision, method)
$ex = new NumberExtension(); $ex->formatNumberToHumanReadable(1200000); // => '1.2M' $ex->formatNumberToHumanReadable(999.99); // => '1K' $ex->formatNumberToHumanReadable(3154.14159, 2, 'ceil'); // => 3.16K' $ex->formatNumberToHumanReadable(3154.14159, 2, 'floor'); // => 3.15K
{{ 1200000|number_to_human_readable }} {# '1.2M' #}