Skip to content

[TwigBundle] make date formats and number formats configurable #13554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2015

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Jan 30, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #13552
License MIT
Doc PR TODO

This adds new Twig configuration options that make it possible to
configure the format of both numbers and dates as well as timezones
without the need to write custom code.

For example, using the new configuration options can look like this:

twig:
    date:
        format: d.m.Y, H:i:s
        interval_format: %%d days
        timezone: Europe/Berlin
    number_format:
        decimals: 2
        decimal_point: ,
        thousands_separator: .

@stof
Copy link
Member

stof commented Jan 30, 2015

this implementation is wrong. the Twig_Environment should not be configured through the TwigEngine, as this means that the twig service is not configured fully whne using it directly rather than using the templating component

@xabbuh
Copy link
Member Author

xabbuh commented Jan 30, 2015

I see your point, but do we have a better place where we can call methods of the Twig extensions?

@stof
Copy link
Member

stof commented Jan 30, 2015

@xabbuh Well, there is 2 solutions here:

  • registering the Twig_Extension_Core explicitly (so that we have a private service for it)
  • using a service configurator to apply the logic. An example can be found in DoctrineBundle used there

->arrayNode('number_format')
->addDefaultsIfNotSet()
->children()
->scalarNode('decimals')->defaultValue(0)->end()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be an integer node

@xabbuh
Copy link
Member Author

xabbuh commented Jan 30, 2015

@stof Thanks for pointing me towards service configurators.

@@ -165,5 +166,14 @@
<argument type="service" id="http_kernel" />
<argument>%twig.exception_listener.controller%</argument>
</service>

<service id="twig.configurator.environment" class="Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator">
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, we cannot make the service private because it then gets inlined and the XML dumper fails here: https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php#L182

The error message then is:

ContextErrorException: Warning: DOMElement::setAttribute() expects parameter 2 to be string, object given in /var/www/symfony/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php line 182

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting the second argument to a string should fix the issue properly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it won't. The issue is that the XML format cannot accept inline services for the configurator, and so cannot handle the case of private configurator service definitions getting inlined in place where they are used

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho the same would be true for other formats as well since the definition does not contain the service id.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh the PHP dumper already supports this case. And the YamlDumper is irrelevant, because it is already unusable on a compiled container (the YAML config format does not support inline service definitions in arguments for instance)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look at #13557 for a proposal about making it possible to use inlined configurator services in the XML format.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that factories and configurators can now be inlined, this is not an issue anymore and I made the service private.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you need to update the depdency to require the DependencyInjection component version where this has been fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we really need to do this. It would mean to add a conflict rule to the Composer config. The issue has been solved in the latest patch version of all maintained Symfony versions though. How did we deal with things like this in the past?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't pay enough attention to such things so far. But to be correct it would be required.

@xabbuh
Copy link
Member Author

xabbuh commented Mar 23, 2015

ping @symfony/deciders

->children()
->scalarNode('format')->defaultNull()->end()
->scalarNode('interval_format')->defaultNull()->end()
->scalarNode('timezone')->defaultNull()->end()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be documented that null means date_default_timezone_get

@xabbuh xabbuh force-pushed the issue-13552 branch 3 times, most recently from c7716eb to 82b249b Compare March 27, 2015 15:03
@xabbuh
Copy link
Member Author

xabbuh commented Mar 27, 2015

I addressed @Tobion's last comments and added an entry to the changelog.

@Tobion
Copy link
Contributor

Tobion commented Mar 27, 2015

@stof what do you think about #13554 (comment)

@xabbuh
Copy link
Member Author

xabbuh commented Mar 27, 2015

By the way, why is the DependencyInjection component not a mandatory requirement? Does this make sense at all?

@xabbuh
Copy link
Member Author

xabbuh commented Mar 31, 2015

ping @symfony/deciders Would be nice if we could get this into 2.7. Bumping the DependencyInjection version imho could be discussed afterwards.

@Tobion
Copy link
Contributor

Tobion commented Mar 31, 2015

👍

1 similar comment
@aitboudad
Copy link
Contributor

👍

$container->setParameter('twig.number_format.decimal_point', $config['number_format']['decimal_point']);
$container->setParameter('twig.number_format.decimals', $config['number_format']['decimals']);
$container->setParameter('twig.number_format.thousands_separator', $config['number_format']['thousands_separator']);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All those parameters should be removed and instead, you should inject the value in the service directly. That's what we are doing now as much as possible to avoid polluting the parameters with information you don't need to access.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<argument />
<argument />
<argument />
<argument />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe to not confuse developers looking where those arguments are set add comment like:

<!-- Above arguments are set in TwigExtension -->

This adds new Twig configuration options that make it possible to
configure the format of both numbers and dates as well as timezones
without the need to write custom code.

For example, using the new configuration options can look like this:

```yaml
twig:
    date:
        format: d.m.Y, H:i:s
        interval_format: %%d days
        timezone: Europe/Berlin
    number_format:
        decimals: 2
        decimal_point: ,
        thousands_separator: .
```
@fabpot
Copy link
Member

fabpot commented Apr 3, 2015

Thank you @xabbuh.

@fabpot fabpot merged commit e9bc23b into symfony:2.7 Apr 3, 2015
fabpot added a commit that referenced this pull request Apr 3, 2015
…igurable (xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

[TwigBundle] make date formats and number formats configurable

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13552
| License       | MIT
| Doc PR        | TODO

This adds new Twig configuration options that make it possible to
configure the format of both numbers and dates as well as timezones
without the need to write custom code.

For example, using the new configuration options can look like this:

```yaml
twig:
    date:
        format: d.m.Y, H:i:s
        interval_format: %%d days
        timezone: Europe/Berlin
    number_format:
        decimals: 2
        decimal_point: ,
        thousands_separator: .
```

Commits
-------

e9bc23b make date formats and number formats configurable
@xabbuh xabbuh deleted the issue-13552 branch April 3, 2015 12:41
@King2500
Copy link
Contributor

King2500 commented Apr 6, 2015

This is fine. As long as you dont have multiple locales. Wouldn't it make more sense to configure it (additionally?) via translation files?

@smolinari
Copy link

I made that same argument here: #13552 (comment)

and got no love.

I agree there needed to be a better overall way to generally make changing formats easier, however, I see it the same way as @King2500 .

Date, number and even currency formats should be part of ( are defined by?) the end user's locale. If you only need one format, then you most likely only have your application working with users who live in one locale (if there are other reasons for changing the formatting in an application globally, please let me know, as I don't understand the reasoning/ use cases). If you have different users with different locales using the application, then you have to use different locale formats. How does this improvement work with those other locale formats?

Also, if you actually do need to use different formatting schemes than the locales offer (and again, I'd love to understand why this might be necessary in a global perspective), then the locale definition is what should be overridable (or possibly make custom locale definitions? Though, that makes even less sense...hmmmm.).

Or actually, this extension

http://twig.sensiolabs.org/doc/extensions/intl.html

has the right logic and I think should actually be part of the core twig package (of an internationally usable templating system). With it in the core, then the locales should be configurable as to which locales are used in the application (defined by each user) and/ or which are overridden or new or customized and how. So that way, when you create templates with variables needing formatting, Twig already takes the user's locale (or the special overridden formatting) into consideration and outputs the formatting accordingly (yes, I'd even leave out the filters and only use a filter, if overriding in the template is actually necessary).

If the addition of the extension to the core causes "too much overhead" and why others that don't really need it can avoid using it (and why it is an extension?), then I'd venture to say, the whole formatting system in Twig needs to be revisited. It wasn't created with an "international templating system" in mind, which any template system should be first and foremost.

Scott

nicolas-grekas added a commit that referenced this pull request Jul 11, 2017
…igs (xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

[TwigBundle] allow to configure custom formats in XML configs

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13554
| License       | MIT
| Doc PR        |

Commits
-------

5a3a24b allow to configure custom formats in XML configs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants