schmittjoh/twig.js — Issue #12: Variables and if statements
Author: Shaked
Created: 2012-02-02 13:00:44+00:00
State: open

Hey

When I use an if statement and inside it I want to use a variable, TwigJS will compile it while it won't create the variable in the right place, for example:

Twig
```

{% if (abc.rack > 1) %}
{{ myVariable }}
{% endif %}

- {{ myVariable }} -

```

JS
```
if (((twig.attr(tmp, "rack") > 1)) {
.
.
.
var myVariable = .....
}
// line 166
sb.append("\t\n \t

-");
// line 167
sb.append(twig.attr(myVariable, "...", ...., "array"));
// line 168
sb.append("\t\n \t -

");
```

Sorry for deleting some data but it was not relevant. Anyway the idea is that if **twig.attr(tmp,"rack")** is not greater then 1 **myVaribale** is not defined although it should be defined **before** the if statement

Current solution is:

Twig
```

{% set myVariable = myVariable %}
{% if (abc.rack > 1) %}
{{ myVariable }}
{% endif %}

- {{ myVariable }} -

```

JS results:

```
var myVariable = ...
if (((twig.attr(tmp, "rack") > 1)) {
.
.
.
// USE myVariable here because it already defined..
}
// line 166
sb.append("\t\n \t

-");
// line 167
sb.append(twig.attr(myVariable, "...", ...., "array"));
// line 168
sb.append("\t\n \t -

");
```

Thanks