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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-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;
/**
* 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;
}
* 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;
}
*/
@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();
}
}