Skip to content

Commit a1197f7

Browse files
committed
ISSUE-201: added a new operator for use with template inheritance. {{?name}}{{/name}} will only be included in the template if the "name" section is overridden.
1 parent b278969 commit a1197f7

File tree

8 files changed

+43
-57
lines changed

8 files changed

+43
-57
lines changed

compiler/src/main/java/com/github/mustachejava/DefaultMustacheVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public void name(TemplateContext templateContext, String variable, Mustache must
5858
list.add(new ExtendNameCode(templateContext, df, mustache, variable));
5959
}
6060

61+
@Override
62+
public void checkName(TemplateContext templateContext, String variable, Mustache mustache) {
63+
list.add(new ExtendCheckNameCode(templateContext, df, mustache, variable));
64+
}
65+
6166
@Override
6267
public void partial(TemplateContext tc, final String variable) {
6368
TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine());

compiler/src/main/java/com/github/mustachejava/MustacheParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
136136
case '#':
137137
case '^':
138138
case '<':
139+
case '?':
139140
case '$': {
140141
boolean oldStartOfLine = startOfLine;
141142
startOfLine = startOfLine & onlywhitespace;
@@ -159,6 +160,9 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
159160
case '$':
160161
mv.name(new TemplateContext(sm, em, file, line, startOfLine), variable, mustache);
161162
break;
163+
case '?':
164+
mv.checkName(new TemplateContext(sm, em, file, line, startOfLine), variable, mustache);
165+
break;
162166
}
163167
iterable = lines != 0;
164168
break;

compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public interface MustacheVisitor {
2828

2929
void name(TemplateContext templateContext, String variable, Mustache mustache);
3030

31+
void checkName(TemplateContext templateContext, String variable, Mustache mustache);
32+
3133
void comment(TemplateContext templateContext, String comment);
3234
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.mustachejava.codes;
2+
3+
import com.github.mustachejava.DefaultMustacheFactory;
4+
import com.github.mustachejava.Mustache;
5+
import com.github.mustachejava.TemplateContext;
6+
7+
import java.io.Writer;
8+
import java.util.List;
9+
10+
/**
11+
* Name a section: {{$name}}...{{/name}}
12+
*/
13+
public class ExtendCheckNameCode extends DefaultCode {
14+
private boolean suppressed;
15+
16+
public ExtendCheckNameCode(TemplateContext templateContext, DefaultMustacheFactory df, Mustache mustache, String variable) {
17+
super(templateContext, df, mustache, variable, "$");
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
}

compiler/src/main/java/com/github/mustachejava/codes/ExtendCode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ private Code[] replaceCodes(Code[] supercodes, Map<String, ExtendNameCode> repla
4343
enc.setCodes(replaceCodes(enc.getCodes(), replaceMap, seen));
4444
}
4545
} else {
46+
if (code instanceof ExtendCheckNameCode) {
47+
ExtendCheckNameCode ecnc = (ExtendCheckNameCode) code;
48+
if (!replaceMap.containsKey(ecnc.getName())) {
49+
code.setCodes(new Code[0]);
50+
}
51+
}
4652
Code[] codes = code.getCodes();
4753
if (codes != null) {
4854
code.setCodes(replaceCodes(codes, replaceMap, seen));

compiler/src/test/java/com/github/mustachejava/InterpreterTest.java

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -926,61 +926,7 @@ public void testRelativePathFail() throws IOException {
926926
}
927927

928928
public void testVariableInhertiance() throws IOException {
929-
DefaultMustacheFactory mf = new DefaultMustacheFactory(root) {
930-
@Override
931-
public MustacheVisitor createMustacheVisitor() {
932-
DefaultMustacheVisitor visitor = new DefaultMustacheVisitor(this) {
933-
@Override
934-
public void extend(TemplateContext templateContext, String variable, Mustache mustache) {
935-
list.add(new ExtendCode(templateContext, df, mustache, variable) {
936-
@Override
937-
public synchronized void init() {
938-
// Find the comments that the pragma generates
939-
List<Code> comments = new ArrayList<>();
940-
for (Code code : mustache.getCodes()) {
941-
if (code instanceof CommentCode) {
942-
comments.add(code);
943-
}
944-
}
945-
super.init();
946-
// Put the comments at the start of the base codes
947-
Code[] codes = partial.getCodes();
948-
if (!comments.isEmpty()) {
949-
Code[] newcodes = new Code[comments.size() + codes.length];
950-
for (int i = 0; i < comments.size(); i++) {
951-
newcodes[i] = comments.get(i);
952-
}
953-
System.arraycopy(codes, 0, newcodes, comments.size(), codes.length);
954-
partial.setCodes(newcodes);
955-
}
956-
957-
}
958-
});
959-
}
960-
};
961-
visitor.addPragmaHandler("set", (tc, pragma, args) -> {
962-
// Create a comment code that sets variables
963-
int index = args.indexOf("=");
964-
if (index == -1) {
965-
throw new MustacheException("Pragme 'set' must have varname=value as an argument");
966-
}
967-
String name = args.substring(0, index);
968-
String value = args.substring(index + 1);
969-
Map<String, String> variable = new HashMap<String, String>() {{
970-
put(name, value);
971-
}};
972-
return new CommentCode(tc, null, "") {
973-
@Override
974-
public Writer execute(Writer writer, List<Object> scopes) {
975-
scopes.add(variable);
976-
return writer;
977-
}
978-
};
979-
});
980-
return visitor;
981-
}
982-
};
983-
929+
DefaultMustacheFactory mf = createMustacheFactory();
984930
Mustache m = mf.compile("issue_201/chat.html");
985931
StringWriter sw = new StringWriter();
986932
m.execute(sw, new Object()).close();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#jsUrl}}<script src="{{jsUrl}}"></script>{{/jsUrl}}
1+
{{?jsUrl}}<script src="{{$jsUrl}}default{{/jsUrl}}"></script>{{/jsUrl}}{{?jsUrl2}}<script src="{{$jsUrl}}default{{/jsUrl}}"></script>{{/jsUrl2}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{{<base.html}}
2-
{{%set jsUrl=test}}
2+
{{$jsUrl}}test{{/jsUrl}}
33
{{/base.html}}

0 commit comments

Comments
 (0)