Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Partial #149

Open
wants to merge 3 commits into
base: mustache.java-0.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.mustachejava.codes.IterableCode;
import com.github.mustachejava.codes.NotIterableCode;
import com.github.mustachejava.codes.PartialCode;
import com.github.mustachejava.codes.DynamicPartialCode;
import com.github.mustachejava.codes.ValueCode;
import com.github.mustachejava.codes.WriteCode;
import com.github.mustachejava.util.Node;
Expand Down Expand Up @@ -82,6 +83,12 @@ public void partial(TemplateContext tc, final String variable) {
TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine());
list.add(new PartialCode(partialTC, df, variable));
}

@Override
public void dynamicPartial(TemplateContext tc, final String variable) {
TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine());
list.add(new DynamicPartialCode(partialTC, df, variable));
}

@Override
public void value(TemplateContext tc, final String variable, boolean encoded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
sm = delimiters.substring(1, length / 2);
em = delimiters.substring(length / 2, length - 1);
break;
case '+':
out = write(mv, out, file, currentLine.intValue(), startOfLine);
startOfLine = startOfLine & onlywhitespace;
mv.dynamicPartial(new TemplateContext(sm, em, file, currentLine.get(), startOfLine), variable);
break;
default: {
if (c == -1) {
throw new MustacheException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ public interface MustacheVisitor {
void extend(TemplateContext templateContext, String variable, Mustache mustache);

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

void dynamicPartial(TemplateContext paramTemplateContext,
String paramString);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.github.mustachejava.codes;

import com.github.mustachejava.*;

import java.io.IOException;
import java.io.Writer;

public class DynamicPartialCode extends DefaultCode {
protected final String extension;
protected final String dir;
protected Mustache partial;
protected int recrusionLimit;

protected DynamicPartialCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String type, String variable) {
super(tc, df, mustache, variable, type);
// Use the name of the parent to get the name of the partial
String file = tc.file();
int dotindex = file.lastIndexOf(".");
extension = dotindex == -1 ? "" : file.substring(dotindex);
int slashindex = file.lastIndexOf("/");
dir = file.substring(0, slashindex + 1);
recrusionLimit = df.getRecursionLimit();
}

public DynamicPartialCode(TemplateContext tc, DefaultMustacheFactory cf, String variable) {
this(tc, cf, null, ">", variable);
}

@Override
public void identity(Writer writer) {
try {
if (name != null) {
super.tag(writer, type);
}
appendText(writer);
} catch (IOException e) {
throw new MustacheException(e);
}
}

@Override
public Code[] getCodes() {
return partial == null ? null : partial.getCodes();
}

@Override
public void setCodes(Code[] newcodes) {
partial.setCodes(newcodes);
}

@Override
public Writer execute(Writer writer, final Object[] scopes) {
DepthLimitedWriter depthLimitedWriter;
if (writer instanceof DepthLimitedWriter) {
depthLimitedWriter = (DepthLimitedWriter) writer;
} else {
depthLimitedWriter = new DepthLimitedWriter(writer);
}
if (depthLimitedWriter.incr() > recrusionLimit) {
throw new MustacheException("Maximum partial recursion limit reached: " + recrusionLimit);
}
try{
if(get(scopes)!=null)
partial = df.compilePartial(get(scopes) +".html");
}catch(Exception e){
throw new MustacheException("Failed to compile partial: " + name);
}
if (partial == null) {
throw new MustacheException("Failed to compile partial: " + name);
}
Writer execute = partial.execute(depthLimitedWriter, scopes);
depthLimitedWriter.decr();
return appendText(execute);
}

@Override
public synchronized void init() {
filterText();
}

/**
* Builds the file name to be included by this partial tag. Default implementation ppends the tag contents with
* the current file's extension.
*
* @return The filename to be included by this partial tag
*/
protected String partialName() {
return df.resolvePartialPath(dir, name, extension);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.mustachejava;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class DyanmicPartialTest {
static Map<String, String> obj = new HashMap<String, String>();

@Test
public void testAbstractClass() throws IOException {

obj.put("firstName", "Check");
obj.put("lastName", "Mate");
obj.put("blogURL", "is something");
obj.put("parent", "partial");

PrintWriter pw = new PrintWriter(System.out);
MustacheFactory mustachefactory = new DefaultMustacheFactory();
Mustache mustacheTemplate = mustachefactory.compile("index.html");
mustacheTemplate.execute(pw, obj).close();
}
}
8 changes: 8 additions & 0 deletions compiler/src/test/resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<script id="personTpl" type="text/template">
<h1>{{firstName}} is {{lastName}}</h1>

{{+ parent }}
</script>

</html>
1 change: 1 addition & 0 deletions compiler/src/test/resources/parent.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Parent is: <a href="{{blogURL}}">{{blogURL}}</a></p>
2 changes: 2 additions & 0 deletions compiler/src/test/resources/partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>Dynamic Partial is: <a href="{{blogURL}}">{{blogURL}}</a></p>