Skip to content

Commit af5a7b9

Browse files
nyonjknack
authored andcommitted
Readding callee check. (#634)
* Render blocks in the parent context and assure a block is passed to templates that use @partial-block * Fixed checkstyle errors. * Readded lazy evaluation. * Readded inline partial evaluation. * Added option to turn lazy partial evaluation on and off * Fix checkstyle errors. * Revert to original whitespaces * Revert to original whitespace * Added tests for lazy partial block evaluation * Renamed lazy to pre and inverted booleans. * Fix for issue 588
1 parent ced0423 commit af5a7b9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

handlebars/src/main/java/com/github/jknack/handlebars/internal/Partial.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.FileNotFoundException;
2424
import java.io.IOException;
2525
import java.io.Writer;
26+
import java.util.Arrays;
2627
import java.nio.charset.Charset;
2728
import java.util.Collections;
2829
import java.util.HashMap;
@@ -123,6 +124,16 @@ protected void merge(final Context context, final Writer writer)
123124
Map<String, Template> inlineTemplates = partials.getLast();
124125
Template callee = context.data(Context.CALLEE);
125126

127+
final boolean pathIsPartialBlock = "@partial-block".equals(path);
128+
final Template lastPartialBlock = inlineTemplates.get("@partial-block");
129+
final boolean parentIsNotLastPartialBlock = !isCalleeOf(callee, lastPartialBlock);
130+
131+
if (pathIsPartialBlock && parentIsNotLastPartialBlock) {
132+
throw new IllegalArgumentException(
133+
callee + " does not provide a @partial-block for " + this
134+
);
135+
}
136+
126137
if (this.partial != null) {
127138
if (handlebars.preEvaluatePartialBlocks()) {
128139
this.partial.apply(context);
@@ -197,6 +208,23 @@ protected void merge(final Context context, final Writer writer)
197208
}
198209
}
199210

211+
/**
212+
* @param callee parent template of the currently traversed template
213+
* @param partialBlock partial block candidate
214+
* @return returns if callee and partialBlock are the same
215+
*/
216+
private boolean isCalleeOf(final Template callee, final Template partialBlock) {
217+
if (callee == null || partialBlock == null) {
218+
return false;
219+
}
220+
221+
if (!callee.filename().equalsIgnoreCase(partialBlock.filename())) {
222+
return false;
223+
}
224+
225+
return Arrays.equals(callee.position(), partialBlock.position());
226+
}
227+
200228
/**
201229
* True, if the file was already processed.
202230
*

handlebars/src/main/java/com/github/jknack/handlebars/internal/PartialBlockForwardingTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public PartialBlockForwardingTemplate(
6666
this.block = block;
6767
this.parentPartialBlock = parentPartialBlock;
6868
this.callee = callee;
69-
this.filename(block.filename());
69+
this.filename(parent.filename());
7070
this.position(parent.position()[0], parent.position()[1]);
7171
}
7272

0 commit comments

Comments
 (0)