Commit Graph

1706 Commits

Author SHA1 Message Date
Fabien Potencier e1ad2bde9c moved filters/functions/tests syntax errors to the parser 2012-11-14 14:33:08 +01:00
Fabien Potencier 2243a14e3d switched version to 1.12 2012-11-14 14:33:03 +01:00
Fabien Potencier f85076b091 added support for extended ternary operator syntaxes (closes #134) 2012-11-14 07:40:53 +01:00
Fabien Potencier b16765ce8f bumped version to 1.11.2-DEV 2012-11-11 18:22:10 +01:00
Fabien Potencier e81b98944f prepared the 1.11.1 release v1.11.1 2012-11-11 18:17:59 +01:00
Fabien Potencier 1a10ea2fbb tweaked previous merge (refs #894) 2012-11-08 10:25:11 +01:00
Fabien Potencier 818f9c7e02 merged branch char101/fix-debug-lineno (PR #894)
This PR was squashed before being merged into the master branch (closes #894).

Commits
-------

7c5854b Fix twig error lineno off by 2

Discussion
----------

Fix twig error lineno off by 2

It seems to me that the lineno reported by Twig_Error is off by 2 from the real lineno in the generated PHP source

For example:

```
{% if true %}
Yes
{% endif %}
```

```php
<?php

/*  */
class __TwigTemplate_d41d8cd98f00b204e9800998ecf8427e extends Twig_Template
{
    public function __construct(Twig_Environment $env)
    {
        parent::__construct($env);

        $this->parent = false;

        $this->blocks = array(
        );
    }

    protected function doDisplay(array $context, array $blocks = array())
    {
        // line 1
        if (true) { // <- this is line 19
            // line 2
            echo "Yes // <- this is line 21
";
        }
    }

    public function getTemplateName()
    {
        return null;
    }

    public function isTraitable()
    {
        return false;
    }

    public function getDebugInfo()
    {
        return array (  19 => 2,  17 => 1,); // <- should be return array (  21 => 2,  19 => 1,);
    }
}
```

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

by char101 at 2012-11-08T08:23:57Z

Since `$compiler->addDebugInfo()` is called first before writing the node content, usually the number of lines in the PHP code is less than 2 (1 for the // lineno comment, and 1 for the generated content) than the line where the node content is written.
2012-11-08 10:22:28 +01:00
Charles 7c5854bba9 Fix twig error lineno off by 2 2012-11-08 10:22:28 +01:00
Fabien Potencier 615804bf74 fixed regression when calling a macro inside another one (closes #889) 2012-11-08 08:57:26 +01:00
Fabien Potencier d7fed092b1 fixed typo 2012-11-08 08:51:01 +01:00
Fabien Potencier 6ece65e3b8 udpated CHANGELOG 2012-11-08 07:31:43 +01:00
Fabien Potencier 501791cf1a added a test for previous merge (refs #892) 2012-11-08 07:29:16 +01:00
Fabien Potencier 769ae43633 merged branch char101/fix-ext-crash-on-undef-func-inside-macro (PR #892)
This PR was merged into the master branch.

Commits
-------

55b76c1 Fix extension crash when calling unknown method inside a macro

Discussion
----------

Fix extension crash when calling unknown method inside a macro

 Issue #890
2012-11-08 07:25:44 +01:00
Fabien Potencier 06bdc7a0ec fixed CS 2012-11-08 07:24:19 +01:00
Fabien Potencier 2fb6a914ce merged branch char101/optimize-strict-vars-php54 (PR #893)
This PR was squashed before being merged into the master branch (closes #893).

Commits
-------

f397a8f Use ternary operator with PHP 5.4 when strict_variables is true

Discussion
----------

Use ternary operator with PHP 5.4 when strict_variables is true

When strict variables is true, and using PHP 5.4, we can still use the ternary operator to optimize performance.
2012-11-08 07:21:48 +01:00
Charles f397a8f79b Use ternary operator with PHP 5.4 when strict_variables is true 2012-11-08 07:21:48 +01:00
Charles 55b76c1799 Fix extension crash when calling unknown method inside a macro 2012-11-08 09:25:27 +07:00
Fabien Potencier 5d87c321bf merged branch nomack84/fixed_typo (PR #891)
This PR was merged into the master branch.

Commits
-------

cc65c88 [template_from_string]Fixed typo

Discussion
----------

[template_from_string]Fixed typo in docs

Fix a typo in the template_from_string function docs
2012-11-07 21:11:21 +01:00
Mario A. Alvarez Garcia cc65c8843a [template_from_string]Fixed typo 2012-11-07 14:59:42 -05:00
Fabien Potencier c248887296 bumped version to 1.11.0-DEV 2012-11-07 12:22:35 +01:00
Fabien Potencier 795f6d876a prepared the 1.11.0 release v1.11.0 2012-11-07 12:20:21 +01:00
Fabien Potencier e6b3f49787 merged branch char101/fix-build-warning (PR #886)
This PR was merged into the master branch.

Commits
-------

3164ee3 Fix build warning for const pointer

Discussion
----------

Fix build warning for const pointer

 Issue #883
2012-11-07 07:34:36 +01:00
Charles 3164ee33d4 Fix build warning for const pointer 2012-11-07 08:49:50 +07:00
Fabien Potencier 58f2a8dfb7 added a .gitignore to ignore files created when building the C extension 2012-11-06 13:20:59 +01:00
Fabien Potencier cfb341f4ba merged branch char101/nativeext-exception-info (PR #885)
This PR was merged into the master branch.

Commits
-------

db13b66 Pass lineno and filename to Twig_Error constructor
feee667 Merge branch 'nativeext-exception-info' of https://github.com/fabpot/Twig into nativeext-exception-info
adb31d4 Call Twig_Error constructor
d4a8c8b added tests for exceptions thrown in Twig_Template::getAttribute()
1b82bf7 Add template filename for the rest of the exception
5675140 Handle NULL filename
71f64bc Add template name to error message

Discussion
----------

Add template name to error message

Issue #884

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

by fabpot at 2012-11-06T07:43:16Z

For some unknown reasons, I don't see your branch when I try to submit a PR. Can you cherry-pick my unit tests from my `nativeext-exception-info` branch?

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

by Tobion at 2012-11-06T08:29:19Z

@char101 Maybe you could also integrate the fixes of #878?

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

by fabpot at 2012-11-06T08:30:55Z

@Tobion Let's do one thing at a time.

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

by char101 at 2012-11-06T08:59:30Z

@Tobion AFAIK, unlike the PHP implementation, the C implementation does not automatically convert array key type.

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

by char101 at 2012-11-06T09:27:54Z

@fabpot How about the lineno variable? Should I just set it to 0 or is there any function that can be called to obtain it?

```php
if (-1 === $this->lineno || null === $this->filename) {
    $this->guessTemplateInfo();
}
```

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

by fabpot at 2012-11-06T09:32:09Z

You need to keep `-1` for the line.

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

by char101 at 2012-11-06T09:36:56Z

If `$lineno` is `-1`, wouldn't `$this->guessTemplateInfo()` still be called?

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

by fabpot at 2012-11-06T09:45:34Z

Yes, but the template name will not be guessed. So, there is a performance overhead for the line guessing, but not for the template name guessing.
2012-11-06 12:54:36 +01:00
Charles db13b66774 Pass lineno and filename to Twig_Error constructor 2012-11-06 16:51:20 +07:00
Charles feee667737 Merge branch 'nativeext-exception-info' of https://github.com/fabpot/Twig into nativeext-exception-info 2012-11-06 15:51:54 +07:00
Charles adb31d4012 Call Twig_Error constructor 2012-11-06 15:50:00 +07:00
Fabien Potencier d4a8c8b2fb added tests for exceptions thrown in Twig_Template::getAttribute() 2012-11-06 08:40:39 +01:00
Charles 1b82bf74fd Add template filename for the rest of the exception 2012-11-06 11:37:50 +07:00
Charles 5675140c9e Handle NULL filename 2012-11-06 11:16:51 +07:00
Charles 71f64bc83b Add template name to error message 2012-11-06 11:02:59 +07:00
Fabien Potencier abec35b833 fixed macro compilation when a variable name is a PHP reserved keyword (closes #881) 2012-11-05 08:52:04 +01:00
Fabien Potencier a009821dd3 fixed CS 2012-11-03 08:27:41 +01:00
Fabien Potencier 96b3ce6bd6 merged branch fabpot/timezones (PR #876)
This PR was merged into the master branch.

Commits
-------

f99504a changed the date filter behavior to always apply the default timezone, except if false is passed as the timezone
d3b0cbd merged branch jmikola/patch-1 (PR #845)
a6a1ef0 Avoid setting timezones on DateIntervals

Discussion
----------

changed the date filter behavior to always apply the default timezone, except if false is passed as the timezone

This PR is based on #845, and addresses issue #778.

Right now, the timezone management when using the date filter is counter-intuitive. When you pass a DateTime object, the default timezone is not applied, but in all other cases, it is.

This PR makes the behavior more consistent by always applying the default timezone (which is probably what users want most of the time), and this can be disabled by explicitly passing ``false`` as the timezone value.

This is a BC break but I think that not many consciously rely on the current behavior.

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

by fabpot at 2012-10-30T15:49:13Z

@jmikola Can you have a look at this PR and tell me your opinion about this change?

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

by jmikola at 2012-11-02T20:17:30Z

@fabpot: Will take a look at this tonight. I missed the notifications as I was offline for a few days.

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

by jmikola at 2012-11-03T04:09:54Z

I read through all of the issues and PR's and this looks reasonable. Are we missing a test case for passing in a DateTime object with `false` for the timezone? I do see one example where we render the current date using the `'e'` format string and provide `false` for the timezone, but that's it.

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

by fabpot at 2012-11-03T07:09:08Z

ok, I've added an additional test just to be sure.
2012-11-03 08:09:43 +01:00
Fabien Potencier f99504ae02 changed the date filter behavior to always apply the default timezone, except if false is passed as the timezone 2012-11-03 08:08:37 +01:00
Fabien Potencier 0879472d7e added a paragraph in the docs about how the Twig blocks work (based on an explanation from nikic, closes #704, refs #351) 2012-10-30 19:13:42 +01:00
Fabien Potencier bcf850da50 fixed a stupid test 2012-10-30 18:12:52 +01:00
Fabien Potencier 1be2495906 fixed broken test 2012-10-30 17:46:49 +01:00
Fabien Potencier 8c5853e3b1 fixed bitwise operator precedences (closes #866) 2012-10-30 17:40:56 +01:00
Fabien Potencier bf3802bc6e merged branch fabpot/template_from_string (PR #874)
This PR was merged into the master branch.

Commits
-------

4599188 added the template_from_string function

Discussion
----------

added the template_from_string function

One of the most often asked question is how someone can evaluate a template string from a template.

The template_from_string function solves this problem (more in the included docs).

I have two questions before merging this:

 * What about the name? I find it quite long but also readable (`{% include template_from_string(template) %}`).

 * Does it belongs to Twig core?

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

by boutell at 2012-10-28T16:45:08Z

Interesting. What are the use cases? Would it cache in some way, the md5 of the string maybe, to avoid having this be a major performance hit on every use?

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

by gcoguiec at 2012-10-28T16:46:04Z

And what about include_from_string ?

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

by lsmith77 at 2012-10-28T16:46:35Z

Heh, was just going to ask the same thing, would a use case be loading a twig snippet from a database? something like https://github.com/symfony-cmf/ContentBundle/issues/8 ..

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

by stof at 2012-10-28T16:48:09Z

@gcoguiec It is not including anything but creating a Twig_Template instance. ``{% extends include_from_string(foo) %}`` would be very ugly.

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

by gruzilla at 2012-10-28T16:48:37Z

i'd say yes.
im not like the core must can do everything. but besides that, what are the reasons against it?
i can already think of some usecases: user defined dynamic templates, ...

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

by alexandresalome at 2012-10-28T16:49:34Z

IMHO it's a mistake to put it in Twig. People won't get how to use it. My reasons are also regarding cache: will the template be regenerated on each call?

Looks magic

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

by stof at 2012-10-28T16:50:06Z

@fabpot I'm not really sure this should be in the core.
But FYI, there is a [PR on your Twig-extensions repository](https://github.com/fabpot/Twig-extensions/pull/68) adding an ``eval`` function with the same goal (but an incomplete implementation)

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

by fabpot at 2012-10-28T16:51:00Z

The template is loaded like any other ones, so it is using the cache if you have configured it. No magic included!

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

by fabpot at 2012-10-28T16:52:44Z

@lsmith77 yes, that's the main use case.

You can also have a look at this thread on the mailing-list: https://groups.google.com/group/twig-devs/browse_thread/thread/a8b7bbae31cade18

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

by fabpot at 2012-10-28T16:54:28Z

I forgot to say that we can also move this function into its own extension, so that enabling it would have to come from a conscious choice from the developer (like the debug function in the debug extension).

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

by lsmith77 at 2012-10-28T16:55:32Z

OK great. Looks like something the CMF will need then. But it would also work for us if its part of the twig extension repo. we could of course also just drop the code into https://github.com/symfony-cmf/CoreBundle/blob/master/Twig/TwigExtension.php

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

by fabpot at 2012-10-28T16:56:59Z

I have proposed it for inclusion in Twig core (but not necessarily in the core extension) as it seems to be something many developers want to do in their code (mainly people writing a CMS in top of Twig).

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

by dlsniper at 2012-10-28T17:05:45Z

Hi

- why not name it `template_string` or `string_template`? Then it would read like: ` {% include template_string(template) %} ` or ` {% include string_template(template) %} `

- imo this shouldn't be in the core but rather in the extensions part of Twig as you just extend existing Twig functionality :)

Cheers.

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

by fabpot at 2012-10-28T17:37:12Z

I've moved the function to a new extension that should be enabled explicitely.

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

by EvanK at 2012-10-28T18:05:57Z

I would love to see this as a built-in feature to Twig, as my coworkers & I have had to implement a workaround for something similar.

(Our workaround involved writing said string to a file named after an md5 of the string's content, and then calling render() on said file, all within the php userspace. It was less than elegant, but functional.)

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

by Crell at 2012-10-28T19:11:34Z

This would lend itself to some of the wacky stuff Drupal has been discussing, too.  +1 on the feature.  No opinion on the name.

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

by jorgelbg at 2012-10-28T20:45:37Z

+1 for built-in support i think this is something Twig should do out of the box, regarding the name I guess that I'll vote for this name ```{% include from_string(template) %}``` The only thing that you can include is a template righ? So why put this explicitly in the function's name?

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

by pylebecq at 2012-10-29T09:53:05Z

+1 for me. And for the name, it seems I'm the only one but I would have vote for `eval` 😈

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

by sstok at 2012-10-29T10:14:25Z

eval is bad name as can be confused with eval() in PHP, aka that what is passed is treated as PHP (similar to embeded PHP in Smarty). Please don't..

+1 for as it is now.

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

by drak at 2012-10-29T17:25:24Z

Love it +1

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

by acasademont at 2012-10-30T12:02:40Z

Very useful feature! +1

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

by acasademont at 2012-10-30T12:20:46Z

For clarification, now if you wanted to make dynamic include or dynamic inheritance like this ```{% extends some_var %}``` you had to supply a valid Twig_Template object. Now with this function you can supply a simple string that will be evaluated and transformed into a Twig_Template object. So this PR is not inventing dynamic inheritance but providing a helper to make it easier. Am I right?

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

by stof at 2012-10-30T12:45:08Z

@acasademont no. It is provinding a way to evaluate a string stored somewhere (in a database for instance) as a template. It is not about making dynamic inheritance easier.
If your template can be loaded by your loader (for instance a file when using the Twig_Loader_Filesystem), you can use the template name in your variable for the dynamic inheritance. You are not required to build the Twig_Template instance yourself

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

by fabpot at 2012-10-30T12:53:02Z

When you have `{% extends some_var %}`, `some_var` must be the template name. Using `{% extends template_from_string(some_var) %}` means that `some_var` contains the parent template code.

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

by acasademont at 2012-10-30T13:15:37Z

Perfectly understood, thanks!
2012-10-30 16:42:59 +01:00
Fabien Potencier 1881045806 added a note about double-escaping when using a variable for the strategy (closes #868) 2012-10-30 10:27:26 +01:00
Fabien Potencier d3b0cbdcb4 merged branch jmikola/patch-1 (PR #845)
This PR was merged into the master branch.

Commits
-------

a6a1ef0 Avoid setting timezones on DateIntervals

Discussion
----------

Avoid setting timezones on DateIntervals

Edit: originally, I opened this to ensure the default Twig timezone was always set on incoming DateTime objects, but I came to realize why that behavior would be undesirable. The idea was prompted by this line from: http://twig.sensiolabs.org/doc/filters/date.html

> The default timezone can also be set globally by calling `setTimezone()`.

Anyway, I did notice an edge case where the code might call `DateInterval::setTimezone()`, so this PR attempts to correct that. Additionally, I added some tests for passing a DateTimeZone object to the `date` filter.

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

by jmikola at 2012-09-22T17:18:56Z

@fabpot: One question came up as I was looking through this a second time: if `twig_date_converter()` receives a DateTime argument, it will override its timezone regardless of the logic path within (either from the `$timezone` argument, Twig default, or the system default).

At first, I thought that it was redundant for `twig_date_format_filter()` not to utilize `twig_date_converter()`, but it looks like that was a careful decision; otherwise, there'd be no way to display DateTimes with their internal timezone. This PR would remove that functionality, so I need to reconsider.

Having said that, there does appear to be a bug in `twig_date_format_filter()` in that it could end up calling a nonexistent `setTimezone()` method on a DateInterval object. Would it be preferable to throw an exception or simply ignore the `$timezone` argument if the `$date` argument is a DateInterval?

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

by fabpot at 2012-10-30T08:56:31Z

I don't know if not changing the timezone on DateTime object was a conscious/careful decision or not. The documentation is not so clear about that, and there is even a bug report about this behavior: #778.

For consistency, I think we can always set the timezone and allow disabling that by passing `false` for the timezone.

Does it sounds good?
2012-10-30 09:57:52 +01:00
Fabien Potencier e3830c70aa fixed default timezone usage for the date function (refactor of the previous merge) 2012-10-30 09:53:04 +01:00
Fabien Potencier ea42eb4dd1 merged branch vitman/patch-1 (PR #871)
This PR was squashed before being merged into the master branch (closes #871).

Commits
-------

b89163d Update lib/Twig/Extension/Core.php

Discussion
----------

Update lib/Twig/Extension/Core.php

Added support for setted default timezone for twig date functions.

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

by henrikbjorn at 2012-10-26T07:19:02Z

This have the same problem as the other PR you created. See php.net/datetime for reference.
2012-10-30 09:40:23 +01:00
vitman b89163df0c Update lib/Twig/Extension/Core.php 2012-10-30 09:40:22 +01:00
Fabien Potencier 459918827b added the template_from_string function 2012-10-29 11:42:14 +01:00
Fabien Potencier 0505c2fefd added two new recipes 2012-10-28 15:30:18 +01:00
Fabien Potencier d59642c949 added missing documentation about Twig_Loader_Chain 2012-10-28 15:19:48 +01:00
Fabien Potencier 42df784d04 fixed phpdoc 2012-10-28 14:36:27 +01:00