Commits
-------
f5b9df9 added `if` modifier support to for loop like {% for k in v if k is odd %}
Discussion
----------
[1.2] added `if` modifier support to for loop
Hi.
I'm one of Twig users who love it so much. :)
I've made a patch for adding `if` modifier support to `for` loop in Twig.
Example:
<pre>
{% for n in range(0, 5) if n is odd %}
{{ n }}
{% endfor %}
</pre>
Output:
<pre>
1
3
5
</pre>
This idea comes from Python's list comprehensions. (like `[n for n in range(0,6) if n % 2 == 1]`)
Here is another example.
Before:
<pre>
{% set no_items = true %}
{% for item in items %}
{% if item.available %}
{% set no_items = false %}
{{ item.name }}
{% endif %}
{% endfor %}
{% if no_items %}
No items available.
{% endif %}
</pre>
After:
<pre>
{% for item in items if item.available %}
{{ item.name }}
{% else %}
No items available.
{% endfor %}
</pre>
I hope you like it. But If not, just ignore this request. :)
Thanks.
---------------------------------------------------------------------------
by fabpot at 2011/06/24 01:33:33 -0700
I like it! I will schedule it for inclusion in Twig 1.2. Thanks.
---------------------------------------------------------------------------
by hhamon at 2011/06/26 16:43:26 -0700
+1
---------------------------------------------------------------------------
by nikic at 2011/06/27 00:42:14 -0700
I think the idea per se is interesting. But I just checked all Twig templates in my whole codebase and couldn't find any use for it. What was your practical use for this feature @kotas?
---------------------------------------------------------------------------
by kotas at 2011/06/27 01:52:48 -0700
@nikic My practical use is shown as the second example in the body of this pull request.
Sometimes you need to "filter" an array by seeing its element's property like "item.available" or by some condition like "item.price >= 500".
To do this, in the current version of Twig, you have to write "for" and "if" as nested scopes. And if you want to write {% else %} of the "for" loop, you have to make a temporary variable to see if all elements are skipped.
I think this feature is not essential, but makes templates clean.
---------------------------------------------------------------------------
by nikic at 2011/06/27 06:23:24 -0700
The thing about `else` seems plausible. +1
---------------------------------------------------------------------------
by chucktrukk at 2011/08/02 17:39:16 -0700
Also +1. that makes some very clean template code.
Commits
-------
755ea99 Correction of a BC break with json_encode and PHP 5.2.x
Discussion
----------
BC break with json_encode and php 5.2.x
Hi,
json_encode on php 5.2.x doesn't take the $options parameter wich result in a warning and the function return nothing.
Cheers.
Commits
-------
7b154d6 Switched a tab to four spaces
29d3cb2 Added testcase for #3954fc7a8d Merge remote-tracking branch 'upstream/master'
c890cf2 Fixed existing tests
9822ac4 Changed Module.php to pass existing tests
fab8c72 fixed issue #395
Discussion
----------
Fix for issue #395
Fix for #395
I hope you can use it.
---------------------------------------------------------------------------
by fabpot at 2011/07/25 00:07:50 -0700
Can you add some unit tests?
---------------------------------------------------------------------------
by netzhuffle at 2011/07/25 13:57:55 -0700
Here you are.
Commits
-------
351910b added twig comments
2b7e8d9 added phpDocs for TokenParsers, filters and tests
Discussion
----------
PHPDocs updated
fixed copy/paste errors and added docs to Sandbox and Use tags
Commits
-------
e4b8371 Improve performance of is defined in non strict mode
Discussion
----------
Improve performance of is defined in non strict mode
Addresses issue #380: This gives a good performance improvement on defined tests in non-strict code. About 2x.
Reasoning: It doesn't make sense to propagate the `is_defined_test` flag into deeper levels in non-strict mode, because all getAttr functions and name accesses will just return `null` if it doesn't exist and `null` is just as good as `false`.
---------------------------------------------------------------------------
by nikic at 2011/07/02 03:22:53 -0700
Though, do not merge yet please. Changes behavior concerning methods.
---------------------------------------------------------------------------
by nikic at 2011/07/02 13:34:32 -0700
Hm, I really don't know what I was thinking when I wrote my last comment. There shouldn't be any behavior changes introduced by that change (and tests pass).
Commits
-------
5b91f12 Update tests
a5ef326 Use Template->getContext in non-strict mode too
Discussion
----------
Use Template->getContext() when in non-strict mode, too
As described in #380 using the current `isset($context['test']) ? $context['test'] : null` approach can cause problems in some cases: The ternary operator always returns by value and thus PHP's copy-on-write concept does not apply. So `$context['test']` needs to be copied in any case, even though a write never happens to it. Basically Twig is copying the values of all variables ever used inside Twig if it operates in non-strict mode. This is problematic when `$context['test']` contains big arrays as the whole array is copied.
In this patch I change Twig to use `Template->getContext` when in non-strict mode too and add an `isStrictVariables` check in there.
Commits
-------
75748a5 revert accidental mode change
681d2b8 fix nested buffers issue #374
Discussion
----------
fix nested buffers issue #374
see #374 for details
---------------------------------------------------------------------------
by fabpot at 2011/07/09 01:02:50 -0700
You have inadvertently changed the mode to 0755. Should stay at 0644.
---------------------------------------------------------------------------
by macolu at 2011/07/09 03:07:42 -0700
Mode change reverted.
About this Windows bug... Are you talking about the zlib issue? When zlib compression is activated, it adds a buffer level that ob_end_clean() can't close. So ob_get_level() never go below 1.
http://cksource.com/forums/viewtopic.php?t=21653http://www.php.net/manual/fr/function.ob-get-level.php#52945
In that case, the first ob_get_level() call will always return something greater than 0. So $level var will be > 0. This should avoid infinite loops.