mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 10:56:38 +00:00
feature #3178 Add twig/string-extra extension (nicolas-grekas, fabpot)
This PR was merged into the 2.x branch. Discussion ---------- Add twig/string-extra extension Last step before deprecating https://github.com/twigphp/Twig-extensions entirely. Commits -------00e0b531Add missing docse300e379Add twig/string-extra extension
This commit is contained in:
@@ -21,6 +21,7 @@ install:
|
||||
- ([[ $TRAVIS_PHP_VERSION = 7.0 ]] || cd extra/inky-extra && travis_retry composer install)
|
||||
- ([[ $TRAVIS_PHP_VERSION = 7.0 ]] || cd extra/intl-extra && travis_retry composer install)
|
||||
- ([[ $TRAVIS_PHP_VERSION = 7.0 ]] || cd extra/markdown-extra && travis_retry composer install)
|
||||
- ([[ $TRAVIS_PHP_VERSION < 7.2 ]] || cd extra/string-extra && travis_retry composer install)
|
||||
|
||||
script:
|
||||
- ./vendor/bin/simple-phpunit
|
||||
@@ -29,6 +30,7 @@ script:
|
||||
- ([[ $TRAVIS_PHP_VERSION = 7.0 ]] || cd extra/inky-extra && ./vendor/bin/simple-phpunit)
|
||||
- ([[ $TRAVIS_PHP_VERSION = 7.0 ]] || cd extra/intl-extra && ./vendor/bin/simple-phpunit)
|
||||
- ([[ $TRAVIS_PHP_VERSION = 7.0 ]] || cd extra/markdown-extra && ./vendor/bin/simple-phpunit)
|
||||
- ([[ $TRAVIS_PHP_VERSION < 7.2 ]] || cd extra/string-extra && ./vendor/bin/simple-phpunit)
|
||||
|
||||
jobs:
|
||||
fast_finish: true
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
* 2.12.1 (2019-XX-XX)
|
||||
|
||||
* n/a
|
||||
* added the String extension in the "extra" repositories: "u" filter
|
||||
|
||||
* 2.12.0 (2019-10-05)
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* added support for an "arrow" function on the "sort" filter
|
||||
* added the CssInliner extension in the "extra" repositories: "inline_css"
|
||||
filter
|
||||
* added the Inky extension in the "extra" repositories: "inky_to_html" filter
|
||||
* added the Inky extension in the "extra" repositories: "inky_to_html" filter
|
||||
* added Intl extension in the "extra" repositories: "country_name",
|
||||
"currency_name", "currency_symbol", "language_name", "locale_name",
|
||||
"timezone_name", "format_currency", "format_number",
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
``u``
|
||||
=====
|
||||
|
||||
.. versionadded:: 2.12.1
|
||||
The ``u`` filter was added in Twig 2.12.1.
|
||||
|
||||
The ``u`` filter wraps a text in a Unicode object (a `Symfony UnicodeString
|
||||
instance <https://symfony.com/doc/current/components/string.html>`_) that
|
||||
exposes methods to "manipulate" the string.
|
||||
|
||||
Let's see some common use cases.
|
||||
|
||||
Wrapping a text to a given number of characters:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ 'Symfony String + Twig = <3'|u.wordwrap(5) }}
|
||||
Symfony
|
||||
String
|
||||
+
|
||||
Twig
|
||||
= <3
|
||||
|
||||
Truncating a string:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ 'Lorem ipsum'|u.truncate(8) }}
|
||||
Lorem ip
|
||||
|
||||
{{ 'Lorem ipsum'|u.truncate(8, '...') }}
|
||||
Lorem...
|
||||
|
||||
Converting a string to *snake* case or *camelCase*:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ 'SymfonyStringWithTwig'|u.snake }}
|
||||
symfony_string_with_twig
|
||||
|
||||
{{ 'symfony_string with twig'|u.camel.title }}
|
||||
SymfonyStringWithTwig
|
||||
|
||||
You can also chain methods:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{{ 'Symfony String + Twig = <3'|u.wordwrap(5).upper }}
|
||||
SYMFONY
|
||||
STRING
|
||||
+
|
||||
TWIG
|
||||
= <3
|
||||
|
||||
For large strings manipulation, use the ``apply`` tag:
|
||||
|
||||
.. code-block:: twig
|
||||
|
||||
{% apply u.wordwrap(5) %}
|
||||
Some large amount of text...
|
||||
{% endapply %}
|
||||
|
||||
.. note::
|
||||
|
||||
The ``u`` filter is part of the ``StringExtension`` which is not installed
|
||||
by default. Install it first:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ composer req twig/string-extra
|
||||
|
||||
Then, use the ``twig/extra-bundle`` on Symfony projects or add the extension
|
||||
explictly on the Twig environment::
|
||||
|
||||
use Twig\Extra\String\StringExtension;
|
||||
|
||||
$twig = new \Twig\Environment(...);
|
||||
$twig->addExtension(new StringExtension());
|
||||
@@ -0,0 +1,4 @@
|
||||
vendor/
|
||||
composer.lock
|
||||
phpunit.xml
|
||||
.phpunit.result.cache
|
||||
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2019 Fabien Potencier
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,11 @@
|
||||
String Extension
|
||||
================
|
||||
|
||||
This package is a Twig extension that provides integration with the Symfony
|
||||
String component.
|
||||
|
||||
It provides a single [`u`][1] filter that wraps a text in a `UnicodeString`
|
||||
object to give access to [methods of the class][2].
|
||||
|
||||
[1]: https://twig.symfony.com/u
|
||||
[2]: https://symfony.com/doc/current/components/string.html
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"name": "twig/string-extra",
|
||||
"type": "library",
|
||||
"description": "A Twig extension for Symfony String",
|
||||
"keywords": ["twig", "html", "string", "unicode"],
|
||||
"homepage": "https://twig.symfony.com",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Fabien Potencier",
|
||||
"email": "fabien@symfony.com",
|
||||
"homepage": "http://fabien.potencier.org",
|
||||
"role": "Lead Developer"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.2.9",
|
||||
"symfony/string": "^5.0@dev",
|
||||
"twig/twig": "^2.4|^3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/phpunit-bridge": "^4.4@dev"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4" : {
|
||||
"Twig\\Extra\\String\\" : "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4" : {
|
||||
"Twig\\Extra\\String\\Tests\\" : "tests/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.12-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
bootstrap="vendor/autoload.php"
|
||||
failOnRisky="true"
|
||||
failOnWarning="true"
|
||||
>
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1" />
|
||||
</php>
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="Twig String Extension Test Suite">
|
||||
<directory>./tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./</directory>
|
||||
<exclude>
|
||||
<directory>./tests</directory>
|
||||
<directory>./vendor</directory>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Extra\String;
|
||||
|
||||
use Symfony\Component\String\UnicodeString;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
final class StringExtension extends AbstractExtension
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('u', [$this, 'createUnicodeString']),
|
||||
];
|
||||
}
|
||||
|
||||
public function createUnicodeString(string $text): UnicodeString
|
||||
{
|
||||
return new UnicodeString($text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
--TEST--
|
||||
"u" filter
|
||||
--TEMPLATE--
|
||||
{{ 'Symfony String + Twig = <3'|u|raw }}
|
||||
|
||||
{{ 'Symfony String + Twig = <3'|u.wordwrap(5).upper|raw }}
|
||||
|
||||
{{ 'SymfonyStringWithTwig'|u.snake }}
|
||||
{{ 'symfony_string with twig'|u.camel.title }}
|
||||
|
||||
{{ 'Lorem ipsum'|u.truncate(8, '...') }}
|
||||
|
||||
--DATA--
|
||||
return []
|
||||
--EXPECT--
|
||||
Symfony String + Twig = <3
|
||||
|
||||
SYMFONY
|
||||
STRING
|
||||
+
|
||||
TWIG
|
||||
= <3
|
||||
|
||||
symfony_string_with_twig
|
||||
SymfonyStringWithTwig
|
||||
|
||||
Lorem...
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Twig.
|
||||
*
|
||||
* (c) Fabien Potencier
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Twig\Extra\String\Tests;
|
||||
|
||||
use Twig\Extra\String\StringExtension;
|
||||
use Twig\Test\IntegrationTestCase;
|
||||
|
||||
class IntegrationTest extends IntegrationTestCase
|
||||
{
|
||||
public function getExtensions()
|
||||
{
|
||||
return [
|
||||
new StringExtension(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getFixturesDir()
|
||||
{
|
||||
return __DIR__.'/Fixtures/';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user