Commit Graph

722 Commits

Author SHA1 Message Date
Eugene Serkin fd78ec0bf1 Fixed PhpDoc for Twig_TokenParserInterface and all other TokenParsers 2011-12-26 01:55:03 +02:00
Fabien Potencier 99b2c5bf99 Revert "merged branch arnaud-lb/hash-key-expr (PR #514)"
This reverts commit 8665c64634, reversing
changes made to 4aba337cda.
2011-12-23 08:59:54 +01:00
Fabien Potencier 8665c64634 merged branch arnaud-lb/hash-key-expr (PR #514)
Commits
-------

542e9e7 added getKeyValuePairs() on Twig_Node_Expression_Array
3f64f1d added test for hash keys
4bdb3a9 fixed tests
3b1f269 support any expression as hash key

Discussion
----------

Allow hash keys to be any expression

This allows hash keys to be any expression

---------------------------------------------------------------------------

by fabpot at 2011/12/07 02:08:19 -0800

I had not implemented this feature back then because I'm not sure it makes sense to support it in the context of a templating system. I'm still not convinced that this is useful.
2011-12-22 22:41:37 +01:00
Fabien Potencier 4aba337cda added a do tag (closes #446) 2011-12-22 22:36:35 +01:00
Fabien Potencier 1d6dfad4f5 added a flush tag 2011-12-21 19:34:14 +01:00
nikic 834ce4c641 Fix loop increment on some for loops (fix #562)
The inner loop incrementing code was not added on for loops where the body was not a Twig_Node (but e.g. a Twig_Node_If)
2011-12-21 11:06:00 +01:00
Fabien Potencier 6f020ccaf4 fixed dump when xdebug.overload_var_dump is not defined in php.ini 2011-12-18 20:54:12 +01:00
Fabien Potencier 4f19c7a04b added support for dynamically named filters and functions 2011-12-18 20:20:35 +01:00
Fabien Potencier 9489136c17 made the dump function registration explicit and disable its output when debug is off 2011-12-18 20:20:35 +01:00
Fabien Potencier 3793740ac5 fixed dump function when the context contains Twig_Template instances 2011-12-18 20:20:35 +01:00
Fabien Potencier 865a7039f8 added a dump function to help debugging templates 2011-12-18 18:30:06 +01:00
Fabien Potencier 11943fbe21 fixed the random function 2011-12-18 15:08:12 +01:00
Fabien Potencier 932a4d8d94 added a nl2br filter 2011-12-18 12:36:07 +01:00
Fabien Potencier 89a1519e09 added a random function 2011-12-18 11:27:47 +01:00
Fabien Potencier f8b7441e42 added a way to change the default format for the date filter 2011-12-18 11:12:59 +01:00
JEDIBC 3bb822ec2f . misspell function _twig_convert_encoding -> twig_convert_encoding 2011-12-09 10:08:13 +01:00
Fabien Potencier 3689745d35 moved some common compiled code to Twig_Template 2011-12-08 19:14:07 +01:00
Fabien Potencier ae250f6b6f fixed the lexer when an operator ending with a letter ends a line 2011-12-08 09:29:20 +01:00
Fabien Potencier 822fe3fa19 moved regexes in the lexer to their own array 2011-12-07 11:07:02 +01:00
Fabien Potencier 1036f90e79 merged branch arnaud-lb/str-interpolation (PR #515)
Commits
-------

d41d10c simplification
2dbb420 moved hard-coded regexes
61986c1 shortcut for non-interpolated double quoted strings
062ed5c regex optimization
0e63abb integration tests for interpolated strings
773fff5 parser tests for interpolated strings
098634e parser support for interpolated strings
c8bba24 lexer tests for interpolated strings
a203686 lexer support for interpolated strings
0bd63dd stack lexer states

Discussion
----------

String interpolation syntax

This adds support for string interpolation:

```
{{ "foo #{ any expression here } bar" }}
```

I find string interpolation to be often more readable than concatenation, and doesn't have the precedence ambiguity of the concatenation operator:

```
Interpolation:

{{ "foo #{bar} baz" }}
{{ "foo #{1+2} baz" }}
{{ "foo #{1+2} baz"|escape }}

Concatenation:

{{ "foo "~bar~" baz" }}
{{ "foo "~(1+2)~" baz" }} extra parentheses
{{ ("foo "~(1+2)~" baz")|escape }} more extra parentheses
```

---------------------------------------------------------------------------

by nikic at 2011/11/12 12:41:54 -0800

Really nice idea. We should discuss the exact syntax though :) Dart for example uses `${expr}` for interpolation or `$name` if the expression is just a name. I think it makes sense to have a special short syntax for the name case, because it is the most common one.

---------------------------------------------------------------------------

by stof at 2011/11/12 13:06:51 -0800

@nikic could you comment on the PR instead of commenting on each commit individually ? it would make it easier to see what is concerned in the PR.

---------------------------------------------------------------------------

by arnaud-lb at 2011/11/12 14:22:14 -0800

@nikic Thanks for your review, I'll take care of your remarks.

For the syntax, I have no preference. I've taken this one because it has the same semantics. In Ruby and Coffeescript the `#{...}` allows any expression between the brackets. In PHP the expression in the `${...}` must start with a variable name. As you mentioned Dart's `${...}` seems to allow any expression.

---------------------------------------------------------------------------

by nikic at 2011/11/12 15:03:48 -0800

@arnaud-lb If Ruby and Coffeescript use this, the #{...} syntax should be okay. Especially as ${...} might be confusing for PHP developers as it has the semantics of variable variables in PHP.

@stof Thanks, will do so next time.

---------------------------------------------------------------------------

by damianb at 2011/11/12 15:38:31 -0800

@stof he doesn't need to - this is something github handles already. in the PR view, beside the "@nikic commented" bits, there's a set of links that shows where exactly the comment was in the format of commit sha, filename, and line inside the diff.

You can see for yourself right here, upper left:
![pr snapshot](http://i42.tinypic.com/zl6s82.png)

---------------------------------------------------------------------------

by stof at 2011/11/13 01:20:38 -0800

@damianb The comment appears, but not the context in which the comment was. We have to follow each link to see it whereas comments done on the PR appear directly with their context. This is why I said it is easier when comments are on the PR.

---------------------------------------------------------------------------

by nikic at 2011/12/02 15:13:30 -0800

So what about this now?

Btw, `REGEX_STRING` and `REGEX_DQ_STRING_PART` still are hardcoded and do not depend on the specified `interpolation` tags. But this looks hard to change, at least the regexes are quite complicated.

---------------------------------------------------------------------------

by arnaud-lb at 2011/12/02 15:22:45 -0800

I think I've addressed all issues (except hardcoding of `#{` and `}` in `REGEX_STRING` and `REGEX_DQ_STRING_PART`.)

Can this be merged after v1.4 is release if the syntax is accepted ?

---------------------------------------------------------------------------

by nikic at 2011/12/03 01:53:41 -0800

This would be my go for `STRING` and `DQ_STRING_PART` (isn't tested):

```php
<?php
$interpolChar = preg_quote(substr($options['interpolation'][0], 0, 1), '/');
$interpolPart = '[^' . $interpolChar . '"\\\\]*';
if (strlen($options['interpolation'][0]) > 1) {
    $interpolRest = preg_quote(substr($options['interpolation'][0], 1), '/');
    $interpolRegex = $interpolPart . '(?:(?:\\\\.|' . $interpolChar . '(?!' . $interpolRest . '))' . $interpolPart . ')*';

} else {
    $interpolRegex =  $interpolPart . '(?:\\\\.' . $interpolPart . ')*';
}
$this->options['dq_string_part_regex'] = '/' . $interpolRegex . '/As';
$this->options['string_regex'] = '/"' . $interpolRegex . '"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As';
```

It should allow both a single char interpolation syntax (like `{`) and multichar ones (like `#{`).

By the way, could we maybe move the compiled regexes out of `$this->options` into `$this->regexes`?
2011-12-07 10:09:34 +01:00
Fabien Potencier 08bda72355 enhanced exceptions for unknown filters, functions, tests, and tags 2011-12-07 09:54:44 +01:00
Fabien Potencier 695bbd276f added a way to get all registered tags 2011-12-07 09:24:13 +01:00
Jonathan Ingram c22c4c6bb8 Removed trailing white spaces 2011-12-07 08:58:57 +01:00
Jonathan Ingram 2c79f494f5 Help the developer when they specify an invalid function by providing some alternatives 2011-12-07 08:58:57 +01:00
Fabien Potencier 55c7ff00b1 merged branch jonathaningram/patch-1 (PR #549)
Commits
-------

989bb22 Removed trailing white spaces
f880bab Help the developer when they specify an invalid filter by providing some alternatives

Discussion
----------

Help the developer when they specify an invalid filter by providing some...

... alternatives.

Bug fix: no
Feature addition: yes
Backwards compatibility break: no

Uses a simple `strpos` search (no fancy diff to determine what the filter could be). There is no impact to runtime performance because this is all happening during template compilation.

Examples:

```jinja
{{ my_entity|json }}
```

Shows exception:

`The filter "json" does not exist. Did you mean "json_encode"?`

```jinja
{{ my_date|dat }}
```

Shows:

`The filter "dat" does not exist. Did you mean "date"?`

```jinja
{{ my_var|f }}
```

Shows:

`The filter "f" does not exist. Did you mean "format", "default", "_default", "format_args", "format_args_as_text", "file_excerpt", "format_file", "format_file_from_text", "file_link"?`

Also good for custom filters created in your own domain `st_`:

```jinja
{{ my_var|st_ }}
```

Shows:

`The filter "st_" does not exist. Did you mean "st_friendly_date", "st_site_test_url"?`

---------------------------------------------------------------------------

by stof at 2011/12/04 16:34:27 -0800

I like the idea to provide help to the developer in case of error. You should probably do the same for functions

---------------------------------------------------------------------------

by Tobion at 2011/12/05 05:56:29 -0800

+1 for a combination of string starts with (suggesting domain specific filters, e.g. st_*) and levenshtein for typos

---------------------------------------------------------------------------

by chucktrukk at 2011/12/05 08:13:10 -0800

+1. This would be really helpful.
2011-12-07 08:19:37 +01:00
Fabien Potencier 460ba4d6c7 bumped version to 1.5.0-DEV 2011-12-07 08:11:23 +01:00
Fabien Potencier 0b7d6dfa0c prepared the 1.4.0 release 2011-12-07 08:09:57 +01:00
Fabien Potencier f1e5278aea merged branch duo-criativa/master (PR #544)
Commits
-------

11b8689 Refactoring: using && instead of nested if's
7aa6757 Bug correction: Parsing integers large than PHP_INT_MAX was generating trucated token values.
046e4ff Bug correction: Parsing integers large than PHP_INT_MAX was generating trucated token values.

Discussion
----------

Fixed bug that was trucating integers large then PHP_INT_MAX

The following code

{% set sizes = [7077888, 452984832, 28991029248, 1855425871872, 9223372036854775807] %}
{% for i in sizes %}
   {{ i }}
{% endfor %}

was generating the following output

7077888
452984832
2147483647
2147483647
2147483647

With the fix, the output looks like the following on Ubuntu 11.04

7077888
452984832
28991029248
1855425871872
9.2233720368548E+18
2011-12-06 16:49:24 +01:00
Jonathan Ingram 989bb22c8f Removed trailing white spaces 2011-12-05 12:45:22 +11:00
Jonathan Ingram f880bab988 Help the developer when they specify an invalid filter by providing some alternatives 2011-12-05 11:01:25 +11:00
Fabien Potencier 6e3a1c4016 added missing preserveKeys argument to the reverse filter 2011-12-03 08:37:53 +01:00
Fabien Potencier c6917a28ef fixed macros containing filter tag calls 2011-12-03 08:16:26 +01:00
Paulo Roberto Ribeiro 11b86893f7 Refactoring: using && instead of nested if's 2011-12-01 20:18:58 -02:00
Paulo Roberto Ribeiro 7aa67572e8 Bug correction: Parsing integers large than PHP_INT_MAX was generating trucated token values. 2011-12-01 18:41:28 -02:00
Paulo Roberto Ribeiro 046e4ffa23 Bug correction: Parsing integers large than PHP_INT_MAX was generating trucated token values. 2011-12-01 17:54:23 -02:00
Martin Hasoň 16d780043f fixed a crash when an object with __toString() method is passed as template name 2011-12-01 12:43:07 +01:00
Arnaud Le Blanc 7b8d476ea5 optimized twig_escape_filter
uses a static variable to avoid initializing the charsets array each
time the function is called; and does a hash lookup instead of array
search (saves a function call too).
2011-11-27 16:13:43 +01:00
Fabien Potencier fba7f95bd2 simplified the implementation of the replace filter 2011-11-27 14:58:36 +01:00
Fabien Potencier 86214c4601 fixed a crash under certain versions of PHP when an object with a __toString() method is used with htmlspecialchars 2011-11-25 19:36:25 +01:00
Fabien Potencier 620b29a4d5 renamed an internal class 2011-11-23 23:45:02 +01:00
Fabien Potencier c90b3ec97a made a small speed optimization 2011-11-23 15:18:40 +01:00
Fabien Potencier 317f6e4725 fixed unit tests due to a difference between the PHP and C versions of Twig_Template::getAttribute() 2011-11-23 14:24:45 +01:00
Fabien Potencier bae4246356 updated Twig ext version 2011-11-23 12:19:09 +01:00
Tobias Schultze 4802f696a3 fixing and improving PHPDoc in Template.php 2011-11-22 09:11:56 +01:00
Fabien Potencier 82e3ecc6da added more unit tests 2011-11-21 14:50:30 +01:00
Pierre Joye 115edf2a71 - fix property access when the instance implements Iterator or similar cases, by using get_object_vars instead, thx Arnaud Leblanc for the hint. 2011-11-21 14:02:02 +01:00
Fabien Potencier 7bc07dc132 removed usage of Reflection in Twig_Template::getAttribute() (thanks Pierre Joye for the property tip) 2011-11-21 10:46:19 +01:00
Fabien Potencier fc6819cec2 added support for twig C extension 2011-11-21 09:36:32 +01:00
Fabien Potencier 9d2272efbf fixed previous merge and added some unit tests 2011-11-21 09:04:06 +01:00
Fabien Potencier b76a574944 merged branch shvchk/patch-2 (PR #522)
Commits
-------

5f30db0 !empty($date[0]) → !empty($date)
0182dbc indentation fix
c7a06cb php warning fix
a4a2d6f Support of negative timestamps

Discussion
----------

Support of negative timestamps

Date could be checked against this condition, as proposed in my patch:

```php
ctype_digit((string) $date)
|| (('-' === $date[0])
    && (ctype_digit(substr($date, 1))))
```
or this:

```php
preg_match('/^\-?\d+/', $date)
```
Second is maybe more readable, but 20% slower.

---------------------------------------------------------------------------

by nikic at 2011/11/20 01:59:16 -0800

Couldn't we do an int validation here? I.e. `if (false !== filter_var($date, FILTER_VALIDATE_INT)) { ... }`. That will allow both positive and negative integers in the allowed integer range. Note though that it will no longer allow ints starting with a zero (apart from zero itself). To support that one would need to pass the ALLOW_OCTAL flag.

Some tests: http://codepad.viper-7.com/w0MDTH

---------------------------------------------------------------------------

by damianb at 2011/11/20 07:50:39 -0800

@nikic why not `ctype_digit`?

---------------------------------------------------------------------------

by nikic at 2011/11/20 07:53:26 -0800

@damianb ctype_digit is what this PR wants to replace, because it does not allow negative numbers. Replacing it with `ctype_digit((string) $date) || (!empty($date[0]) && ('-' === $date[0]) && ctype_digit(substr($date, 1)))` seems like overkill to me though, that's why I proposed to use `false !== filter_var($date, FILTER_VALIDATE_INT)` instead, which apart from being shorter is also semantically more correct.

---------------------------------------------------------------------------

by damianb at 2011/11/20 08:03:09 -0800

@nikic But does filter_var with FILTER_VALIDATE_INT perform better than what's proposed?

Also, I would think that the `empty($date[0])` should just be replaced with an empty check on the `$date` var itself - seems quite pointless to check if the first character is empty, that'd imply that either a) the string itself is empty b) `date[0] == 0`, which could result in a nasty gotcha for someone down the road.

---------------------------------------------------------------------------

by shvchk at 2011/11/20 09:36:17 -0800

@damianb, you are right, I will update the code.
@nikic, on 32-bit systems ```PHP_INT_MAX``` is ```2147483647```, so your code will fail on such systems for 01.01.1900, for example, which is ```-2208988800``` and is not that rarely used.
2011-11-21 08:53:51 +01:00