Skip to content
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

Add a constant() test for testing values against constants. #120

Closed
markstory opened this issue Sep 11, 2010 · 4 comments
Closed

Add a constant() test for testing values against constants. #120

markstory opened this issue Sep 11, 2010 · 4 comments

Comments

@markstory
Copy link
Contributor

Quite a few applications use constants to make various magic numbers
and strings more memorable, and usable. Would it be useful to have a
test in twig allowing you to check values against global and class
constants. Proposed implementation would be:

{% if my_value is constant('ADMIN_LVL') %}
or
{% if post.state is constant('Post::PUBLISHED') %}

@arnaud-lb
Copy link
Contributor

Why not a constant filter instead ? This would allow to retrieve the value of the constant.

e.g. {{ 'ADMIN_LVL'|constant }}
and {% if my_value == 'ADMIN_LVL'|constant %}

@arnaud-lb
Copy link
Contributor

an other use case for retrieving the value of a constant is to use constants as filter arguments, as in {{ myDate|date('DATE_W3C'|constant) }}

@markstory
Copy link
Contributor Author

I guess it could be done as a filter as well. I originally thought of a test because I normally use constants in if statements. I usually think of filters as output manipulation, and I don't normally output constants. But the date() case is interesting.

@timotheemoulin
Copy link

I made my own extension to do that : https://github.com/tehem/TwigConstantExtension

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants