-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from tx-francesco-figari/single-block-rendering
Extend PebbleTemplate to allow rendering of single blocks
- Loading branch information
Showing
6 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/test/java/com/mitchellbosecke/pebble/RenderSingleBlockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.mitchellbosecke.pebble; | ||
|
||
import com.mitchellbosecke.pebble.error.PebbleException; | ||
import com.mitchellbosecke.pebble.loader.StringLoader; | ||
import com.mitchellbosecke.pebble.template.PebbleTemplate; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.Locale.CANADA; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class RenderSingleBlockTest extends AbstractTest { | ||
|
||
@Test | ||
public void testRenderSingleBlock() throws PebbleException, IOException { | ||
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build(); | ||
|
||
String source = "Prefix {% block block_a %}Block A{% endblock %}{% block block_b %}Block B{% endblock %} Postfix"; | ||
PebbleTemplate template = pebble.getTemplate(source); | ||
|
||
Writer writer_a = new StringWriter(); | ||
template.evaluateBlock("block_a", writer_a); | ||
assertEquals("Block A", writer_a.toString()); | ||
|
||
Writer writer_b = new StringWriter(); | ||
template.evaluateBlock("block_b", writer_b); | ||
assertEquals("Block B", writer_b.toString()); | ||
} | ||
|
||
@Test | ||
public void testRenderSingleBlockWithLocale() throws PebbleException, IOException { | ||
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build(); | ||
|
||
String source = "Prefix {% block block_a %}Block A{% endblock %}{% block block_b %}Block B{% endblock %} Postfix"; | ||
PebbleTemplate template = pebble.getTemplate(source); | ||
|
||
Writer writer_a = new StringWriter(); | ||
template.evaluateBlock("block_a", writer_a, CANADA); | ||
assertEquals("Block A", writer_a.toString()); | ||
|
||
Writer writer_b = new StringWriter(); | ||
template.evaluateBlock("block_b", writer_b, CANADA); | ||
assertEquals("Block B", writer_b.toString()); | ||
} | ||
|
||
@Test | ||
public void testRenderSingleBlockWithContext() throws PebbleException, IOException { | ||
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build(); | ||
|
||
String source = "Prefix {% block block_a %}{{vara}}{% endblock %}{% block block_b %}{{varb}}{% endblock %} Postfix"; | ||
PebbleTemplate template = pebble.getTemplate(source); | ||
|
||
Map<String, Object> context = new HashMap<>(); | ||
context.put("vara", "FOO"); | ||
context.put("varb", "BAR"); | ||
|
||
Writer writer_a = new StringWriter(); | ||
template.evaluateBlock("block_a", writer_a, context, CANADA); | ||
assertEquals("FOO", writer_a.toString()); | ||
|
||
Writer writer_b = new StringWriter(); | ||
template.evaluateBlock("block_b", writer_b, context, CANADA); | ||
assertEquals("BAR", writer_b.toString()); | ||
} | ||
|
||
@Test | ||
public void testRenderSingleBlockWithContextAndLocale() throws PebbleException, IOException { | ||
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build(); | ||
|
||
String source = "Prefix {% block block_a %}{{vara}}{% endblock %}{% block block_b %}{{varb}}{% endblock %} Postfix"; | ||
PebbleTemplate template = pebble.getTemplate(source); | ||
|
||
Map<String, Object> context = new HashMap<>(); | ||
context.put("vara", "FOO"); | ||
context.put("varb", "BAR"); | ||
|
||
Writer writer_a = new StringWriter(); | ||
template.evaluateBlock("block_a", writer_a, context); | ||
assertEquals("FOO", writer_a.toString()); | ||
|
||
Writer writer_b = new StringWriter(); | ||
template.evaluateBlock("block_b", writer_b, context); | ||
assertEquals("BAR", writer_b.toString()); | ||
} | ||
|
||
@Test | ||
public void testRenderSingleExtendedBlock() throws PebbleException, IOException { | ||
PebbleEngine pebble = new PebbleEngine.Builder().strictVariables(true).build(); | ||
PebbleTemplate template = pebble.getTemplate("templates/single-block/template.renderextendedblock1.peb"); | ||
|
||
Writer writer_a = new StringWriter(); | ||
template.evaluateBlock("container_a", writer_a); | ||
assertEquals("Block A extended", writer_a.toString()); | ||
|
||
Writer writer_b = new StringWriter(); | ||
template.evaluateBlock("container_b", writer_b); | ||
assertEquals("Block B extended", writer_b.toString()); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/test/resources/templates/single-block/template.renderextendedblock1.peb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{# used by RenderSingleBlockTest.testRenderSingleExtendedBlock #} | ||
{% extends "./template.renderextendedblock2.peb" %} | ||
|
||
Text in the template that should not appear in either block | ||
|
||
{% block content_a %}Block A{% endblock %} | ||
{% block content_b %}Block B{% endblock %} |
6 changes: 6 additions & 0 deletions
6
src/test/resources/templates/single-block/template.renderextendedblock2.peb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{# used by RenderSingleBlockTest.testRenderSingleExtendedBlock #} | ||
|
||
Text in the parent template that should not appear in either block | ||
|
||
{% block container_a %}{% block content_a %}{% endblock %} extended{% endblock %} | ||
{% block container_b %}{% block content_b %}{% endblock %} extended{% endblock %} |