[英]Freemarker : nested templates
I am a beginner with freemarker and I would like to use it to generate some repetitive code. 我是freemarker的初学者,我想用它来生成一些重复的代码。
From a simple class like this one: 从这样一个简单的类中:
public class Point {
private Integer x;
private Integer y;
private String name;
}
I need, for each attribute, to generate lines like this: 我需要为每个属性生成像这样的行:
ValueProvider<Point,Integer> x();
ValueProvider<Point,Integer> y();
ValueProvider<Point,String> name();
To achieve this, I have this simple template : 为此,我有一个简单的模板:
ValueProvider<${clazz},${attrType}> ${attrName}();
Then, I want to generate a complete class like this: 然后,我想生成一个完整的类,如下所示:
public final class PointValueProviders {
public interface PointPropertyAccess extends PropertyAccess<Point>{
ValueProvider<Point,Integer> x();
ValueProvider<Point,Integer> y();
ValueProvider<Point,String> name();
}
public static final PointPropertyAccess POINT_PA= GWT.create(PointPropertyAccess.class);
private PointValueProviders(){}
};
For this, I have a problem : I don't know how to apply the small templates an undetermined number of times in a bigger template like this one: 为此,我有一个问题:我不知道如何在较小的模板(如此模板)中多次应用较小的模板:
public final ${clazz}ValueProviders {
public interface ${clazz}PropertyAccess extends PropertyAccess<${clazz}>{
//Here, How do I tell freemarker to use the small template???
//ValueProvider<${clazz},${attrType}> ${attrName}();
//ValueProvider<${clazz},${attrType}> ${attrName}();
//ValueProvider<${clazz},${attrType}> ${attrName}();
//ValueProvider<${clazz},${attrType}> ${attrName}();
//etc..
}
public static final ${clazz}PropertyAccess ${clazzUpperCase}_PA= GWT.create(${clazz}PropertyAccess.class);
private ${clazz}ValueProviders(){}
};
Any idea? 任何想法?
Templates are to display some data that you provide to them. 模板将显示您提供给它们的一些数据。 So the important question is, how would the template know what
class
/ attrType
/ attrName
trios to output? 因此,重要的问题是,模板如何知道要输出什么
class
/ attrType
/ attrName
三重奏? You should provide a list of those, let's call it props
, and then just loop through it with 您应该提供这些列表,将其称为
props
,然后使用
<#list props as prop>
ValueProvider<${prop.clazz},${prop.attrType}> ${prop.attrName}();
</#list>
Otherwise to create small reusable templates either use #macro
(this one is the more flexible) or #include
. 否则,要创建小型可重用模板,请使用
#macro
(此方法更灵活)或#include
。 See them in the FreeMarker Manual . 在FreeMarker手册中查看它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.