I am using DropWizard and Freemarker to build up a view which displays different types of forms based on results from a webservice.
I have created the forms as views - each with their own ftl.
So, in my resource, I discover which form I need, then load the main.ftl, passing the form view as a parameter (see below).
This doesn't work. Can anyone see where we're going wrong? Or is there a completely different way to chain views together using DropWizard and freemarker?
@GET
public Form getForm() {
FormView view = new FormView(service.getForm());
return new MainView(view);
}
public class FormView extends View {
private final Form form;
public FormView(Form form) {
super("formView.ftl");
this.form = form;
}
public Form getForm() {
return form;
}
}
public class MainView extends View {
private final FormView formView;
public MainView(FormView formView) {
super("main.ftl");
this.formView = formView;
}
public FormView getFormView() {
return formView;
}
}
public class TextForm extends View implements Form {
private int maxLength;
private String text;
public TextForm(int maxLength, String text) {
super("textForm.ftl");
this.maxLength = maxLength;
this.text = text;
}
public int getMaxLength() {
return maxLength;
}
public String getText() {
return text;
}
}
main.ftl
<#-- @ftlvariable formView="" type="MainView" -->
<html>
<body>
<#include formView.templateName /> // This evaluates to formView.ftl, but it seems to create a new instance, and doesn't have the reference to textForm.ftl. How do we reference a dropwizard view here?
</body>
</html>
formView.ftl
<#-- @ftlvariable form type="FormView" -->
${form?html} // also tried #include form.templateName
textForm.ftl
<#-- @ftlvariable text type="TextForm" -->
<div>${text?html}</div>