本文整理汇总了Java中org.jtwig.JtwigModel类的典型用法代码示例。如果您正苦于以下问题:Java JtwigModel类的具体用法?Java JtwigModel怎么用?Java JtwigModel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JtwigModel类属于org.jtwig包,在下文中一共展示了JtwigModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: renderElement
import org.jtwig.JtwigModel; //导入依赖的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.JtwigModel; //导入依赖的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: addOn
import org.jtwig.JtwigModel; //导入依赖的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!"));
}
示例4: extendsInsideFor
import org.jtwig.JtwigModel; //导入依赖的package包/类
@Test
public void extendsInsideFor() throws Exception {
String result = JtwigTemplate.inlineTemplate("{% extends 'memory:a' %}" +
"{% block post %}- {{ item.title }}{% endblock %}", configuration()
.resources().resourceLoaders().add(new TypedResourceLoader(MEMORY, InMemoryResourceLoader.builder()
.withResource("a", "{% for item in posts %}" +
"{% block post %}{{ item.title }}{% endblock %}" +
"{% endfor %}")
.build())).and().and()
.build())
.render(JtwigModel.newModel().with("posts", asList(
new Item("a"),
new Item("b")
)));
assertThat(result, is("- a- b"));
}
示例5: formatWithNow
import org.jtwig.JtwigModel; //导入依赖的package包/类
@Test
public void formatWithNow() throws Exception {
final Date date = new Date();
DateFormatFunction.setDateSupplier(new DateFormatFunction.NowDateSupplier() {
@Override
public Date now() {
return date;
}
});
String result = JtwigTemplate.inlineTemplate("{{ date('now','yyyy-MM-dd hh:mm:ss z', 'Europe/Paris') }}")
.render(JtwigModel.newModel());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss z");
format.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
String expected = format.format(date);
assertThat(result, is(expected));
}
示例6: listeners
import org.jtwig.JtwigModel; //导入依赖的package包/类
@Test
public void listeners() throws Exception {
String result = JtwigTemplate.inlineTemplate("{{ var }}", configuration()
.render().renderListeners()
.add(new StagedRenderListener(RenderStage.PRE_TEMPLATE_RENDER, new RenderListener() {
@Override
public void listen(RenderRequest request) {
request.getRenderContext().getCurrent(ValueContext.class)
.with("var", "test");
}
}))
.and()
.withStrictMode(true)
.and()
.build())
.render(JtwigModel.newModel());
assertThat(result, is("test"));
}
示例7: includingFirstAvailableOfFallbacksFromArrayModel
import org.jtwig.JtwigModel; //导入依赖的package包/类
@Test
public void includingFirstAvailableOfFallbacksFromArrayModel() 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()
);
Map<String, Object> modelData = new HashMap<>();
modelData.put("paths", new String[] {"memory:b", "memory:a"});
String result = template.render(JtwigModel.newModel(modelData));
assertThat(result, is("b"));
}
示例8: allowingForCurrentDirectoryRelativePaths
import org.jtwig.JtwigModel; //导入依赖的package包/类
@Test
public void allowingForCurrentDirectoryRelativePaths() throws Exception {
File parentDirectory = FileUtils.getTempDirectory();
File subDirectory = new File(parentDirectory, "temp");
subDirectory.mkdirs();
File file1 = new File(parentDirectory, "file1.twig");
File file2 = new File(subDirectory, "file2.twig");
FileUtils.write(file1, "{% include './temp/file2.twig' %}");
FileUtils.write(file2, "{{ var }}");
String result = JtwigTemplate.fileTemplate(file1)
.render(JtwigModel.newModel()
.with("var", "Hi")
);
assertThat(result, is("Hi"));
}
示例9: renderEncodingWithNestedConstructsTest
import org.jtwig.JtwigModel; //导入依赖的package包/类
@Test
public void renderEncodingWithNestedConstructsTest() throws Exception {
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
JtwigModel model = JtwigModel.newModel().with("string", "The è");
Charset outputCharset = Charset.forName("Cp858");
JtwigTemplate.inlineTemplate("{{string}}", configuration()
.render().withOutputCharset(outputCharset).and()
.build()).render(model, outputStream1);
JtwigTemplate.inlineTemplate("{% filter void %}{{string}}{% endfilter %}", configuration()
.render().withOutputCharset(outputCharset).and()
.extensions().add(new MyExtension()).and()
.build()).render(model, outputStream2);
assertArrayEquals(outputStream1.toByteArray(), outputStream2.toByteArray());
}
示例10: render
import org.jtwig.JtwigModel; //导入依赖的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);
}
示例11: loadFromTemplateDirectory
import org.jtwig.JtwigModel; //导入依赖的package包/类
/**
* Loads this template data.
*
* @throws InvalidTemplateException If an error occurs while loading the template from the theme directory.
* @throws IOException If an exception occurs while reading the template.
*/
public final void loadFromTemplateDirectory() throws InvalidTemplateException, IOException {
final File pageTemplate = new File(themeDirectory, Constants.FILE_THEME_PAGE_FILE);
if(!pageTemplate.exists() || !pageTemplate.isFile()) {
throw new InvalidTemplateException("\"" + Constants.FILE_THEME_PAGE_FILE + "\" not found.");
}
final JtwigModel model = createModel();
final IncludeFileFunction includeFile = new IncludeFileFunction(themeDirectory, model, false);
template = includeFile.renderIncludeFile(pageTemplate);
}
示例12: createModel
import org.jtwig.JtwigModel; //导入依赖的package包/类
/**
* Creates the corresponding model for this template with some variables.
*
* @param otherVariables The variables.
*
* @return The corresponding JTwig model.
*/
public final JtwigModel createModel(final Map<String, Object> otherVariables) {
final HashMap<String, Object> variables = new HashMap<String, Object>(this.variables);
if(otherVariables != null) {
variables.putAll(otherVariables);
}
return JtwigModel.newModel(variables)
.with(Constants.VARIABLE_GENERATOR_NAME, Constants.APP_NAME)
.with(Constants.VARIABLE_GENERATOR_VERSION, Constants.APP_VERSION)
.with(Constants.VARIABLE_GENERATOR_WEBSITE, Constants.APP_WEBSITE);
}
示例13: getDSL
import org.jtwig.JtwigModel; //导入依赖的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;
}
示例14: createQuery
import org.jtwig.JtwigModel; //导入依赖的package包/类
public String createQuery() {
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/" + templateName);
JtwigModel model = JtwigModel.newModel();
modelParams.forEach(model::with);
return template.render(model);
}
示例15: writeReport
import org.jtwig.JtwigModel; //导入依赖的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");
}