X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FTemplatePart.java;h=ac5694c0f072ad848474c30eed6189ed4b673c55;hb=e5647042f01e94a78f6411216cf77a67f52e1b7a;hp=2b27a895bcb9a3a89c3fbe8190b1ec2321a8706f;hpb=7b5ff1f6607f68d38da4e68d293f8146a3886772;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/text/TemplatePart.java b/src/main/java/net/pterodactylus/sone/text/TemplatePart.java index 2b27a89..ac5694c 100644 --- a/src/main/java/net/pterodactylus/sone/text/TemplatePart.java +++ b/src/main/java/net/pterodactylus/sone/text/TemplatePart.java @@ -18,16 +18,23 @@ package net.pterodactylus.sone.text; import java.io.IOException; +import java.io.StringWriter; import java.io.Writer; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateContextFactory; +import net.pterodactylus.util.template.TemplateException; /** * {@link Part} implementation that is rendered using a {@link Template}. * * @author David ‘Bombe’ Roden */ -public class TemplatePart implements Part { +public class TemplatePart implements Part, net.pterodactylus.util.template.Part { + + /** The template context factory. */ + private final TemplateContextFactory templateContextFactory; /** The template to render for this part. */ private final Template template; @@ -35,10 +42,13 @@ public class TemplatePart implements Part { /** * Creates a new template part. * + * @param templateContextFactory + * The template context factory * @param template * The template to render */ - public TemplatePart(Template template) { + public TemplatePart(TemplateContextFactory templateContextFactory, Template template) { + this.templateContextFactory = templateContextFactory; this.template = template; } @@ -55,8 +65,8 @@ public class TemplatePart implements Part { * The value of the variable * @return This template part (for method chaining) */ - public TemplatePart set(String key, String value) { - template.set(key, value); + public TemplatePart set(String key, Object value) { + template.getInitialContext().set(key, value); return this; } @@ -69,7 +79,33 @@ public class TemplatePart implements Part { */ @Override public void render(Writer writer) throws IOException { - template.render(writer); + template.render(templateContextFactory.createTemplateContext().mergeContext(template.getInitialContext()), writer); + } + + /** + * {@inheritDoc} + */ + @Override + public void render(TemplateContext templateContext, Writer writer) throws TemplateException { + template.render(templateContext.mergeContext(template.getInitialContext()), writer); + } + + // + // OBJECT METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringWriter stringWriter = new StringWriter(); + try { + render(stringWriter); + } catch (IOException ioe1) { + /* should never throw, ignore. */ + } + return stringWriter.toString(); } }