schmittjoh/twig.js — pull request #66 by h2s (2013-05-09 13:32:39+00:00)
Title: Variables and if statements (Issue #12)

Hey there Johannes!

I was wondering, what do you think of this as a potential fix for the problem reported by @Shaked in issue #12?

For some reason, something somewhere isn't keeping track of scope properly, and temporary variables aren't getting redeclared after if blocks. For example, in following example, the PHP compiler assigns `$_example` twice: once within the if block and once afterwards.

```php
// line 1
$context["example"] = 12345;
if (false) {
if (isset($context["example"])) { $_example_ = $context["example"]; } else { $_example_ = null; }
echo twig_escape_filter($this->env, $_example_, "html", null, true);
}
if (isset($context["example"])) { $_example_ = $context["example"]; } else { $_example_ = null; }
echo twig_escape_filter($this->env, $_example_, "html", null, true);
echo "
";
```

The Javascript compiler, on the other hand, is only doing the counterpart `tmp_example` assignment operation *once*: within the the if block. The second one never happens, so the penultimate `sb.append` operation fails.

```javascript
// line 1
context["example"] = 12345;
if (false) {
var tmp_example = ("example" in context) ? context["example"] : null;
sb.append(twig.filter.escape(this.env_, tmp_example, "html", null, true));
}
sb.append(twig.filter.escape(this.env_, tmp_example, "html", null, true));
sb.append("\n");
```

This pull requests fixes this by simply disabling the temporary variable optimization. What do you think, dude? It'd be good to get that 12-month-old issue closed, that's for sure, but I'm also aware that this isn't necessarily the most elegant fix for the problem. Thing is, I doubt you or I have the time or inclination to really dig into this problem and fix it at the root, so maybe this is better than brokenness?