N.B*_*N.B 3 java eclipse templates
我想在java中使用Eclipse IDE创建一个模板文件.我想创建一个模板文件,使得一个程序从用户获取参数,它应该能够将这些参数粘贴到模板文件中,然后将其保存为单独的文件.我怎样才能做到这一点 ?
请指导我.
谢谢NB
以下是我在开源模板引擎Chunk中的表现.
import com.x5.template.Theme;
import com.x5.template.Chunk;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
...
public void writeTemplatedFile() throws IOException
{
Theme theme = new Theme();
Chunk chunk = theme.makeChunk("my_template", "txt");
// replace static values below with user input
chunk.set("name", "Lancelot");
chunk.set("favorite_color", "blue");
String outfilePath = getFilePath();
File file = new File(outfilePath);
FileWriter out = new FileWriter(file);
chunk.render(out);
out.flush();
out.close();
}
Run Code Online (Sandbox Code Playgroud)
my_template.txt(只需放在themes/my_template.txt的classpath中)
My name is {$name}.
My favorite color is {$favorite_color}.
Run Code Online (Sandbox Code Playgroud)
输出:
My name is Lancelot.
My favorite color is blue.
Run Code Online (Sandbox Code Playgroud)
通过添加| filters和:默认值可以使您的模板变得更加智能.
my_template.txt - 示例2
My name is {$name|defang:[not provided]}.
My favorite color is {$favorite_color|defang|lc:[not provided]}.
Run Code Online (Sandbox Code Playgroud)
在此示例中,defang过滤器会删除可能有助于形成XSS攻击的任何字符.该lc过滤器更改文本为小写.如果值为null,则输出冒号之后的任何内容.
有一个Eclipse插件可用于直接在Eclipse IDE中编辑Chunk模板.该插件提供语法突出显示和模板文档的大纲视图.
Chunk可以做更多的事情,看一看文档以便快速浏览.完全披露:我喜欢Chunk,部分原因是因为我创造了它.