Skip to content

Commit

Permalink
Merge pull request #157 from jstachio/parent_partial_master
Browse files Browse the repository at this point in the history
Inheritance support #120
  • Loading branch information
samskivert authored Nov 30, 2023
2 parents 92c1222 + f861f55 commit a697d1c
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 88 deletions.
435 changes: 353 additions & 82 deletions src/main/java/com/samskivert/mustache/Mustache.java

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion src/main/java/com/samskivert/mustache/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Iterator;
import java.util.Map;

import com.samskivert.mustache.Mustache.BlockSegment;

/**
* Represents a compiled template. Templates are executed with a <em>context</em> to generate
* output. The context can be any tree of objects. Variables are resolved against the context.
Expand Down Expand Up @@ -165,7 +167,7 @@ protected Template (Segment[] segs, Mustache.Compiler compiler) {
_fcache = compiler.collector.createFetcherCache();
}

protected Template indent(String indent) {
protected Template indent (String indent) {
// What we want to do here is rebuild this partial template but indented.
// If identing does not change anything we return the original template.
if (indent.equals("")) {
Expand All @@ -178,6 +180,17 @@ protected Template indent(String indent) {
return new Template(copySegs, _compiler);
}

protected Template replaceBlocks (Map<String, BlockSegment> blocks) {
if (blocks.isEmpty()) {
return this;
}
Segment[] copySegs = Mustache.replaceBlockSegs(_segs, blocks);
if (copySegs == _segs) {
return this;
}
return new Template(copySegs, _compiler);
}

protected void executeSegs (Context ctx, Writer out) throws MustacheException {
for (Segment seg : _segs) {
seg.execute(this, ctx, out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class PartialThreadSafeTest {

@Test
public void testPartialThreadSafe() throws Exception {
long t = System.currentTimeMillis();
AtomicInteger loadCount = new AtomicInteger();
TemplateLoader loader = new TemplateLoader() {
@Override
Expand Down Expand Up @@ -64,7 +63,5 @@ public Reader getTemplate(String name) throws Exception {
}
assertTrue(q.isEmpty());
assertEquals(1, loadCount.get());
System.out.println(loadCount);
System.out.println(System.currentTimeMillis() - t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public CustomSpecTest(Spec spec, String name) {
@Parameters(name = "{1}")
public static Collection<Object[]> data () {
String[] groups = new String[] {
"partials"
"sections",
"partials",
"~inheritance"
};
return SpecTest.data("/custom/specs/", groups);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public static Collection<Object[]> data () {
"interpolation",
"inverted",
"sections",
"partials"
"partials",
"~inheritance"
};
return SpecTest.data("/specs/specs/", groups);
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/samskivert/mustache/specs/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package com.samskivert.mustache.specs;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;

/**
Expand All @@ -19,6 +21,7 @@ public Spec (Map<String, Object> map) {
this.map = map;
@SuppressWarnings("unchecked") Map<String, String> partials =
(Map<String, String>) map.get("partials");
if (partials == null) partials = Collections.emptyMap();
this.partials = partials;
}

Expand Down Expand Up @@ -57,6 +60,15 @@ public String toString() {
value.accept(getDescription());
label.accept("template");
value.accept(getTemplate());
if (! partials.isEmpty()) {
label.accept("partials");
sb.append("\n");
for( Entry<String, String> e : partials.entrySet()) {
sb.append("\t").append(e.getKey()).append(":\n");
value.accept(e.getValue());
}
sb.append("\n");
}
label.accept("expected");
value.accept(getExpectedOutput());
return sb.toString();
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/samskivert/mustache/specs/SpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void test () throws Exception {
// specifications, but we throw an exception (and rightfully so IMO; this is not a
// place where silent failure is helpful), so just ignore those test failures
if (!e.getMessage().contains("Invalid delimiter")) {
e.printStackTrace();
Assert.fail(
desc + "\nExpected: " + uncrlf(spec.getExpectedOutput()) + "\nError: " + e);
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/resources/custom/specs/sections.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
overview: |
Section and End Section tags SHOULD be treated as standalone when
appropriate.
tests:
- name: Standalone Lines
desc: Standalone lines should be removed from the template.
data: { boolean: true }
template: |
| This Is
{{#boolean}}{{^missing}}|{{/missing}}{{/boolean}}
| A Line
expected: |
| This Is
|
| A Line
# eof
12 changes: 12 additions & 0 deletions src/test/resources/custom/specs/~inheritance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
overview: |
Custom inheritance tests.
tests:
- name: Non block content should not impact standalone blocks
desc: Content inside a parent call that is not block tags should be ignored
data: { }
template: "{{<parent}}ignore{{$ballmer}}\npeaked\n\n:(\n{{/ballmer}}{{/parent}}"
partials:
parent: "{{$ballmer}}peaking{{/ballmer}}"
expected: "peaked\n\n:(\n"

# eof

0 comments on commit a697d1c

Please sign in to comment.