本文整理汇总了Java中org.jtwig.JtwigTemplate类的典型用法代码示例。如果您正苦于以下问题:Java JtwigTemplate类的具体用法?Java JtwigTemplate怎么用?Java JtwigTemplate使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JtwigTemplate类属于org.jtwig包,在下文中一共展示了JtwigTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: renderElement
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
private String renderElement(Map<String, String> data) {
// Cache template contents
if (template == null) {
if (this.templateAbsolutePath != "") {
System.err.println(
"Load tempate by absolute path: " + this.templateAbsolutePath);
template = JtwigTemplate.fileTemplate(this.templateAbsolutePath);
} else {
System.err
.println("Load tempate by resource path: " + this.templateName);
template = JtwigTemplate.classpathTemplate(this.templateName);
}
}
JtwigModel model = JtwigModel.newModel();
for (String key : data.keySet()) {
// System.err.println(String.format("\"%s\" = \"%s\"", key,
// data.get(key)));
model.with(key, data.get(key).replace("\"", "\\\""));
}
String output = template.render(model);
// System.err.println("renderElement : " + output);
return output;
}
示例2: applyTemplate
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
/**
* Applies the template to a file.
*
* @param file The file.
* @param compress Whether the file should be compressed.
* @param page If the file is page.
* @param otherVariables Other variables to put.
*
* @throws IOException If an exception occurs while saving the file.
*/
public final void applyTemplate(final File file, final boolean compress, DocsPage page, final Map<String, Object> otherVariables) throws IOException {
if(page == null) {
page = new DocsPage(project, file);
}
final JtwigModel model = createModel(otherVariables).with(Constants.VARIABLE_PAGE, page);
final IncludeFileFunction includeFile = new IncludeFileFunction(themeDirectory, model, RANGE_FUNCTION);
final EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder.configuration().functions().add(includeFile).add(RANGE_FUNCTION).and().build();
if(otherVariables != null) {
page.addAdditionalVariables(otherVariables);
}
String content = JtwigTemplate.inlineTemplate(template, configuration).render(model);
if(compress && FilenameUtils.getExtension(file.getPath()).equalsIgnoreCase("html")) {
content = HTML_COMPRESSOR.compress(content);
}
Files.write(file.toPath(), content.getBytes(StandardCharsets.UTF_8));
}
示例3: execute
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Override
public final Object execute(final FunctionRequest functionRequest) {
if(functionRequest.getNumberOfArguments() == 0) {
return "You must specify a file in " + Constants.FUNCTION_INCLUDE_FILE + ".";
}
final String fileName = functionRequest.getArguments().get(0).toString();
try {
final File file = new File(directory.getPath() + File.separator + fileName);
if(!file.exists() || !file.isFile()) {
return "Incorrect path given : " + directory.getPath() + fileName;
}
if(!render) {
return renderIncludeFile(file);
}
final EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder.configuration().functions().add(this).add(Arrays.asList(functions)).and().build();
return JtwigTemplate.fileTemplate(file, configuration).render(model);
}
catch(final Exception ex) {
ex.printStackTrace();
return "Unable to parse " + directory.getPath() + File.separator + fileName + " (" + ex.getClass().getName() + ")";
}
}
示例4: addOn
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Test
public void addOn() throws Exception {
String result = JtwigTemplate.inlineTemplate("{% hello %}", configuration()
.parser().addonParserProviders().add(new AddonParserProvider() {
@Override
public Class<? extends AddonParser> parser() {
return SimpleAddOnParser.class;
}
@Override
public Collection<String> keywords() {
return Collections.emptyList();
}
}).and().and()
.render().nodeRenders().add(SimpleAddOn.class, new AddOnNodeRender()).and().and()
.build()).render(JtwigModel.newModel());
assertThat(result, is("Hello World!"));
}
示例5: select
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Test
public void select() throws Exception {
GroupedTimingStatistics groupedTimingStatistics = new GroupedTimingStatistics();
groupedTimingStatistics.setStartTime(System.currentTimeMillis());
JtwigTemplate jtwigTemplate = JtwigTemplate.inlineTemplate("{{ var.nested.field[0] }}");
for (int i = 0; i < SIZE; i++) {
StopWatch stopWatch = new StopWatch("selection");
String random = RandomStringUtils.random(6);
JtwigModel jtwigModel = newModel().with("var", new SelectionTest.TestClass(random));
stopWatch.start();
jtwigTemplate.render(jtwigModel);
stopWatch.stop();
groupedTimingStatistics.addStopWatch(stopWatch);
}
groupedTimingStatistics.setStopTime(System.currentTimeMillis());
System.out.println(groupedTimingStatistics.toString());
}
示例6: includingFirstAvailableOfFallbacksFromIterableModel
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Test
public void includingFirstAvailableOfFallbacksFromIterableModel() throws Exception {
JtwigTemplate template = JtwigTemplate.inlineTemplate("{% include paths %}", configuration()
.resources().resourceLoaders().add(new TypedResourceLoader(MEMORY, InMemoryResourceLoader
.builder()
.withResource("a", "{{ 'a' }}")
.withResource("b", "{{ 'b' }}")
.build())).and().and()
.build()
);
List<String> pathList = new ArrayList<>();
pathList.add("memory:b");
pathList.add("memory:a");
Map<String, Object> modelData = new HashMap<>();
modelData.put("paths", pathList);
String result = template.render(JtwigModel.newModel(modelData));
assertThat(result, is("b"));
}
示例7: render
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Override
public String render(String templatePath, Map<String, Object> data) throws Exception {
// TODO template cache when startup
JtwigTemplate template = JtwigTemplate.classpathTemplate(templatePath);
JtwigModel model = JtwigModel.newModel(data);
return template.render(model);
}
示例8: renderIncludeFile
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
/**
* Render a String but processing only include file functions.
*
* @param toRender String to render.
*
* @return Rendered String.
*/
public final String renderIncludeFile(String toRender) {
final EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder.configuration().functions().add(this).add(Arrays.asList(functions)).and().build();
final Matcher matcher = PARTIAL_RENDER_PATTERN.matcher(toRender);
while(matcher.find()) {
final String matching = matcher.group();
toRender = toRender.replace(matching, JtwigTemplate.inlineTemplate(matching, configuration).render(model));
}
return toRender;
}
示例9: getDSL
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Override
public String getDSL() {
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/filters.twig");
JtwigModel model = JtwigModel.newModel().with("var", "World");
String result = template.render(model);
logger.info("get search dsl " + result);
return result;
}
示例10: createQuery
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
public String createQuery() {
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/" + templateName);
JtwigModel model = JtwigModel.newModel();
modelParams.forEach(model::with);
return template.render(model);
}
示例11: writeReport
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
/**
* {@inheritDoc}
* <p>
* Render the report HTML using Jtwig and send the result to the recipient emails.
*/
@Override
public void writeReport(List<TestSuite> testSuites) throws IOException {
if (testSuites == null) {
testSuites = Collections.emptyList();
}
log.info("Sending report email for {} test suites", testSuites.size());
List<TestSuiteModel> testList = new ArrayList<>(testSuites.size());
boolean hasError = false;
for (TestSuite testSuite : testSuites) {
TestSuiteModel testSuiteModel = new TestSuiteModel(testSuite);
hasError = hasError || !testSuiteModel.allPassed();
testList.add(testSuiteModel);
}
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/email.twig");
JtwigModel model = JtwigModel.newModel()
.with("error", hasError)
.with("testList", testList);
String reportHtml = template.render(model);
EmailBuilder emailBuilder = new EmailBuilder().from(senderName, fromEmail)
.replyTo(senderName, replyTo)
.subject("Validatar Report – " + (hasError ? "Test Errors" : "Tests Passed"))
.addHeader("X-Priority", 2)
.textHTML(reportHtml);
for (String recipientEmail : recipientEmails) {
emailBuilder.to(recipientEmail);
}
Email reportEmail = emailBuilder.build();
ServerConfig mailServerConfig = new ServerConfig(smtpHost, smtpPort);
Mailer reportMailer = new Mailer(mailServerConfig, TransportStrategy.SMTP_TLS);
sendEmail(reportMailer, reportEmail);
log.info("Finished sending report to recipients");
}
示例12: sortIncomparable
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Test
public void sortIncomparable() throws Exception {
expectedException.expectMessage(containsString("Sort function only works over collections of items, where items implement the interface java.lang.Comparable interface"));
JtwigTemplate.inlineTemplate("{{ var | sort }}")
.render(JtwigModel.newModel().with("var", asList(new Object(), new Object())));
}
示例13: testFalse
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Test
public void testFalse() throws Exception {
JtwigModel model = new JtwigModel();
String result = JtwigTemplate.inlineTemplate("{{ false ? 1 : 2 }}")
.render(model);
assertThat(result, is(equalTo("2")));
}
示例14: lastMap
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Test
public void lastMap() throws Exception {
String result = JtwigTemplate.inlineTemplate("{{ last(var) }}").render(JtwigModel.newModel().with("var", ImmutableMap.builder()
.put("a", 1)
.build()));
assertThat(result, is("1"));
}
示例15: outputWithStripRightWhiteSpace
import org.jtwig.JtwigTemplate; //导入依赖的package包/类
@Test
public void outputWithStripRightWhiteSpace() throws Exception {
JtwigTemplate template = JtwigTemplate.inlineTemplate(" {{ variable -}} ");
String result = template.render(newModel().with("variable", "hello"));
assertThat(result, is(" hello"));
}