merged branch Seldaek/isarray (PR #700)
Commits
-------
c88f8e5 Micro-optimizations
70cca66 Simplify code
d78ed66 Add traversable test
5fd2f98 Add docs
b2e1675 Add is array test
Discussion
----------
is array/traversable tests
It's sometimes necessary to display arbitrary data to be able to know if we should recurse or not.
---------------------------------------------------------------------------
by stof at 2012-04-13T12:13:11Z
I'm not sure about the need for ``is array``. All Twig functions related to arrays also support traversable objects
---------------------------------------------------------------------------
by Seldaek at 2012-04-13T12:23:32Z
Well, I like to have both for explicitness and also because I would try is array without looking at the docs, and I don't think it's right to make is array return true for traversables.
---------------------------------------------------------------------------
by fabpot at 2012-04-20T09:38:33Z
One of the main goal of Twig is to abstract the "real" type of the variables. We have already discussed the introduction of many tests like these ones and I have always rejected them.
As many people seems to have a need for them (especially for arrays), I would consider adding the `traversable` test. But as @stof said, Twig tries to blur the difference between traversable objects and arrays, so I'm -1 for adding another test for arrays.
`traversable` is probably not the best name for web designers but I don't know what else we can use here.
---------------------------------------------------------------------------
by stof at 2012-04-20T09:41:05Z
In Jinja, there is a built-in test for this using ``iterable``: http://jinja.pocoo.org/docs/templates/#builtin-tests
---------------------------------------------------------------------------
by Seldaek at 2012-04-20T09:41:32Z
Ok. I can live with dropping the array test. Now for the name, `is forloopable`? I am kind of at a loss as well apart from traversable. I think if it's clearly mentioned in the docs it's ok.
---------------------------------------------------------------------------
by Seldaek at 2012-04-20T09:52:12Z
iterable sounds ok to me, but just as confusing as traversable I guess to the unknowing. A sidenote, if I update the PR, any objection to adding the "undefined" test as well?
---------------------------------------------------------------------------
by fabpot at 2012-04-20T10:01:27Z
Let's use `iterable` as this is what Jinja already uses. Why would you want an `undefined` test as we already have `defined`? (`if foo is not defined` works well enough)
---------------------------------------------------------------------------
by Seldaek at 2012-04-20T11:16:26Z
I just saw jinja had it on that page, and I thought why not add it too? Still not a huge fan of the "not" for negation.
---------------------------------------------------------------------------
by fabpot at 2012-04-20T12:18:17Z
Well, let's first finish the `iterable` test first and let's discuss `undefined` in another PR or issue.
---------------------------------------------------------------------------
by fabpot at 2012-04-20T17:10:28Z
I can finish the PR is you want.
---------------------------------------------------------------------------
by Seldaek at 2012-04-20T17:13:11Z
If you like sure, it's mostly deleting code anyway. I just have to finish some work stuff now..