Skip to content

Commit 86f0cb1

Browse files
committed
Fixes.
1 parent 9222c3a commit 86f0cb1

File tree

6 files changed

+53
-8
lines changed

6 files changed

+53
-8
lines changed

api-docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
---
44

55
## twig
6-
> version 1.0.2, created by JPPM.
6+
> version 1.0.3, created by JPPM.
77
88
Twig template engine for JPHP
99

1010
### Install
1111
```
12-
12+
1313
```
1414

1515
### API

api-docs/README.ru.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
---
44

55
## twig
6-
> версия 1.0.2, создано с помощью JPPM.
6+
> версия 1.0.3, создано с помощью JPPM.
77
88
Twig template engine for JPHP
99

1010
### Установка
1111
```
12-
12+
1313
```
1414

1515
### АПИ

package.php.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: twig
2-
version: 1.0.2
2+
version: 1.0.3
33
description: Twig template engine for JPHP
44

55
plugins:
@@ -44,6 +44,8 @@ doc:
4444
ru: Русский
4545

4646
history:
47+
1.0.3:
48+
- Fix bug of getting value in forin loop.
4749
1.0.2:
4850
- Add able to return raw text from filters and functions.
4951
- Upgrade pebble lib version.

src-jvm/main/java/php/pkg/twig/classes/TwigTemplate.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.io.StringWriter;
99
import java.util.HashMap;
1010
import java.util.Map;
11+
import java.util.Map.Entry;
1112
import php.pkg.twig.TwigExtension;
13+
import php.runtime.Memory;
1214
import php.runtime.annotation.Reflection.Getter;
1315
import php.runtime.annotation.Reflection.Namespace;
1416
import php.runtime.annotation.Reflection.Signature;
@@ -44,9 +46,18 @@ public String render(Environment env) throws IOException {
4446
return render(env, new HashMap<>());
4547
}
4648

49+
protected void prepareContext(Environment env, Map<String, Object> contexts) {
50+
for (Entry<String, Object> entry : contexts.entrySet()) {
51+
if (entry.getValue() instanceof Memory) {
52+
contexts.put(entry.getKey(), Memory.unwrap(env, (Memory) entry.getValue(), true));
53+
}
54+
}
55+
}
56+
4757
@Signature
4858
public String render(Environment env, Map<String, Object> contexts) throws IOException {
4959
StringWriter writer = new StringWriter();
60+
prepareContext(env, contexts);
5061
getWrappedObject().evaluate(writer, contexts, WrapLocale.getDefault(env));
5162
return writer.toString();
5263
}
@@ -59,6 +70,8 @@ public String renderBlock(Environment env, String block) throws IOException {
5970
@Signature
6071
public String renderBlock(Environment env, String block, Map<String, Object> contexts) throws IOException {
6172
StringWriter writer = new StringWriter();
73+
prepareContext(env, contexts);
74+
6275
getWrappedObject().evaluateBlock(block, writer, contexts, WrapLocale.getDefault(env));
6376
return writer.toString();
6477
}

src-jvm/main/java/php/pkg/twig/support/JPHPExtension.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ private ResolvedAttribute resolve(IObject instance, String attributeName, Object
4444
}
4545
}
4646

47-
return new ResolvedAttribute(Memory.unwrap(env, memory));
47+
return new ResolvedAttribute(Memory.unwrap(env, memory.toImmutable(), true));
4848
} else {
49-
return new ResolvedAttribute(Memory.unwrap(env, instance.callMethodAny(env, attributeName, argumentValues)));
49+
return new ResolvedAttribute(Memory.unwrap(env, instance.callMethodAny(env, attributeName, argumentValues).toImmutable(), true));
5050
}
5151
} catch (Throwable e) {
5252
if (e instanceof BaseError) {
@@ -73,7 +73,7 @@ private ResolvedAttribute resolve(Memory instance, String attributeName, Object[
7373
return null;
7474
}
7575

76-
return new ResolvedAttribute(Memory.unwrap(env, instance.valueOfIndex(attributeName)));
76+
return new ResolvedAttribute(Memory.unwrap(env, instance.valueOfIndex(attributeName).toImmutable(), true));
7777
}
7878
}
7979

tests/BasicTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,34 @@ public function testSafeString()
110110

111111
Assert::isEqual('<html>', $this->twig->renderString('{{ "<html>" | safeString }}'));
112112
}
113+
114+
public function testEquals()
115+
{
116+
Assert::isEqual('success',
117+
$this->twig->renderString(
118+
'{{ x == "1.2.3" ? "success" : "fail" }}',
119+
['x' => '1.2.3']
120+
)
121+
);
122+
}
123+
124+
public function testArrayGetEquals()
125+
{
126+
Assert::isEqual('success',
127+
$this->twig->renderString(
128+
'{{ x[0] == y[0] ? "success" : "fail" }}',
129+
['x' => ['1.2.3'], 'y' => ['1.2.3']]
130+
)
131+
);
132+
}
133+
134+
public function testForInEquals()
135+
{
136+
Assert::isEqual('success',
137+
$this->twig->renderString(
138+
'{% for el in arr %}{{ el == "1.2.3" ? "success" : "fail" }}{% endfor %}',
139+
['arr' => ['1.2.3']]
140+
)
141+
);
142+
}
113143
}

0 commit comments

Comments
 (0)