merged branch jturmel/master (PR #403)
Commits
-------
c2c01de Add bitwise operators to core
Discussion
----------
Added bitwise operators (and, xor, or) to core
* Added bitwise operators (and, xor, or) to core
---------------------------------------------------------------------------
by nikic at 2011/08/02 21:02:14 -0700
I'm not sure bitwise ops really belong into a templateing language...
---------------------------------------------------------------------------
by jturmel at 2011/08/02 21:06:02 -0700
That's a general statement to make without knowing why it was implemented at all... no one is forced to use them, however when you do need them, them not being available makes you have relegate something that should be in the view in the particular case I needed them, to be done in a controller instead, which would be quite a bit nastier in my case.
---------------------------------------------------------------------------
by nikic at 2011/08/02 23:23:30 -0700
@jturmel: I do not doubt that there are some cases where bitwise operations are useful. I can't think of one of the top of my head, though I imagine that it could be useful when using bitwise right management or something like that. What I was trying to say is, that I don't really believe that there are enough significant use cases for bitwise ops to justify adding them to the core (I know, "You Don't Have To Use It!", but it isn't good to clutter the core with features nearly no one uses). Maybe you could give an example of what you needed them for?
---------------------------------------------------------------------------
by jturmel at 2011/08/02 23:41:36 -0700
It would be one thing if I was implementing some operator that didn't normally exist in PHP itself, but it seems a bit odd to even say that it's cluttering it up, it's 3 lines of code that add 3 missing standard operators from PHP.
Simply put, I have an object that gets passed to the template that has many properties, these properties are bit values, I need to do checks on them to select/deselect various checkboxes in a grid... this is the cleanest way to do it without mudding up controller code with view logic.
---------------------------------------------------------------------------
by jalliot at 2011/08/03 01:42:56 -0700
Why not adding ``<<`` and ``>>`` then @jturmel?
I don't have a particular use case but I think that if Twig includes those 3 operators, it should also support these 2 to be "complete". What do you think?
---------------------------------------------------------------------------
by jturmel at 2011/08/03 06:07:46 -0700
Of course, if you guys think we need the shifting operators as well, we can... but modifying data in the view seems much less practical than the one's I added, although I did miss NOT.
---------------------------------------------------------------------------
by jalliot at 2011/08/03 06:16:38 -0700
I forgot about ``~`` but sure it should be included as well.
As for ``<<`` and ``>>``, they could also be used for verifying grant access for example in some implementations.
``{% if some_var << some_other_var & 1 %}``