Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dim-s committed Aug 21, 2018
1 parent 9222c3a commit 86f0cb1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
4 changes: 2 additions & 2 deletions api-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
---

## twig
> version 1.0.2, created by JPPM.
> version 1.0.3, created by JPPM.
Twig template engine for JPHP

### Install
```
jppm add twig@1.0.2
jppm add twig@1.0.3
```

### API
Expand Down
4 changes: 2 additions & 2 deletions api-docs/README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
---

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

### Установка
```
jppm add twig@1.0.2
jppm add twig@1.0.3
```

### АПИ
Expand Down
4 changes: 3 additions & 1 deletion package.php.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: twig
version: 1.0.2
version: 1.0.3
description: Twig template engine for JPHP

plugins:
Expand Down Expand Up @@ -44,6 +44,8 @@ doc:
ru: Русский

history:
1.0.3:
- Fix bug of getting value in forin loop.
1.0.2:
- Add able to return raw text from filters and functions.
- Upgrade pebble lib version.
Expand Down
13 changes: 13 additions & 0 deletions src-jvm/main/java/php/pkg/twig/classes/TwigTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import php.pkg.twig.TwigExtension;
import php.runtime.Memory;
import php.runtime.annotation.Reflection.Getter;
import php.runtime.annotation.Reflection.Namespace;
import php.runtime.annotation.Reflection.Signature;
Expand Down Expand Up @@ -44,9 +46,18 @@ public String render(Environment env) throws IOException {
return render(env, new HashMap<>());
}

protected void prepareContext(Environment env, Map<String, Object> contexts) {
for (Entry<String, Object> entry : contexts.entrySet()) {
if (entry.getValue() instanceof Memory) {
contexts.put(entry.getKey(), Memory.unwrap(env, (Memory) entry.getValue(), true));
}
}
}

@Signature
public String render(Environment env, Map<String, Object> contexts) throws IOException {
StringWriter writer = new StringWriter();
prepareContext(env, contexts);
getWrappedObject().evaluate(writer, contexts, WrapLocale.getDefault(env));
return writer.toString();
}
Expand All @@ -59,6 +70,8 @@ public String renderBlock(Environment env, String block) throws IOException {
@Signature
public String renderBlock(Environment env, String block, Map<String, Object> contexts) throws IOException {
StringWriter writer = new StringWriter();
prepareContext(env, contexts);

getWrappedObject().evaluateBlock(block, writer, contexts, WrapLocale.getDefault(env));
return writer.toString();
}
Expand Down
6 changes: 3 additions & 3 deletions src-jvm/main/java/php/pkg/twig/support/JPHPExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ private ResolvedAttribute resolve(IObject instance, String attributeName, Object
}
}

return new ResolvedAttribute(Memory.unwrap(env, memory));
return new ResolvedAttribute(Memory.unwrap(env, memory.toImmutable(), true));
} else {
return new ResolvedAttribute(Memory.unwrap(env, instance.callMethodAny(env, attributeName, argumentValues)));
return new ResolvedAttribute(Memory.unwrap(env, instance.callMethodAny(env, attributeName, argumentValues).toImmutable(), true));
}
} catch (Throwable e) {
if (e instanceof BaseError) {
Expand All @@ -73,7 +73,7 @@ private ResolvedAttribute resolve(Memory instance, String attributeName, Object[
return null;
}

return new ResolvedAttribute(Memory.unwrap(env, instance.valueOfIndex(attributeName)));
return new ResolvedAttribute(Memory.unwrap(env, instance.valueOfIndex(attributeName).toImmutable(), true));
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,34 @@ public function testSafeString()

Assert::isEqual('<html>', $this->twig->renderString('{{ "<html>" | safeString }}'));
}

public function testEquals()
{
Assert::isEqual('success',
$this->twig->renderString(
'{{ x == "1.2.3" ? "success" : "fail" }}',
['x' => '1.2.3']
)
);
}

public function testArrayGetEquals()
{
Assert::isEqual('success',
$this->twig->renderString(
'{{ x[0] == y[0] ? "success" : "fail" }}',
['x' => ['1.2.3'], 'y' => ['1.2.3']]
)
);
}

public function testForInEquals()
{
Assert::isEqual('success',
$this->twig->renderString(
'{% for el in arr %}{{ el == "1.2.3" ? "success" : "fail" }}{% endfor %}',
['arr' => ['1.2.3']]
)
);
}
}

0 comments on commit 86f0cb1

Please sign in to comment.