Java Code Examples for com.samskivert.mustache.Mustache#TemplateLoader

The following examples show how to use com.samskivert.mustache.Mustache#TemplateLoader . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Meta.java    From TypeScript-Microservices with MIT License 5 votes vote down vote up
/**
 * Creates mustache loader for template using classpath loader
 *
 * @param generator - class with reader getter
 * @return loader for template
 */
private static Mustache.TemplateLoader loader(final DefaultGenerator generator) {
    return new Mustache.TemplateLoader() {
        @Override
        public Reader getTemplate(String name) {
            return generator.getTemplateReader(TEMPLATE_DIR_CLASSPATH + File.separator
                    + name.concat(MUSTACHE_EXTENSION));
        }
    };
}
 
Example 2
Source File: Meta.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Converter method to process supporting files: execute with mustache, or simply copy to
 * destination directory
 *
 * @param targetDir - destination directory
 * @param data - map with additional params needed to process templates
 * @return converter object to pass to lambdaj
 */
private static Converter<SupportingFile, File> processFiles(final File targetDir,
        final Map<String, Object> data) {
    return support -> {
        try {
            File destinationFolder =
                    new File(new File(targetDir.getAbsolutePath()), support.folder);
            File outputFile = new File(destinationFolder, support.destinationFilename);

            TemplateManager templateProcessor = new TemplateManager(
                    new TemplateManagerOptions(false, false),
                    new MustacheEngineAdapter(),
                    new TemplatePathLocator[]{ new CommonTemplateContentLocator("codegen") }
            );

            String template = templateProcessor.readTemplate(new File(TEMPLATE_DIR_CLASSPATH, support.templateFile).getPath());

            String formatted = template;

            Mustache.TemplateLoader loader = name -> templateProcessor.getTemplateReader(name.concat(MUSTACHE_EXTENSION));

            if (support.templateFile.endsWith(MUSTACHE_EXTENSION)) {
                LOGGER.info("writing file to {}", outputFile.getAbsolutePath());
                formatted =
                        Mustache.compiler().withLoader(loader).defaultValue("")
                                .compile(template).execute(data);
            } else {
                LOGGER.info("copying file to {}", outputFile.getAbsolutePath());
            }

            FileUtils.writeStringToFile(outputFile, formatted, StandardCharsets.UTF_8);
            return outputFile;

        } catch (IOException e) {
            throw new RuntimeException("Can't generate project", e);
        }
    };
}
 
Example 3
Source File: JMustacheViewResolver.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
protected Mustache.TemplateLoader loader(final ResourceLoader templateLoader) {
	return name -> {
		String filename = filename(name);
		byte[] bytes = templateLoader.load(filename);
		U.must(bytes != null, "The JMustache template '%s' doesn't exist!", filename);
		return new StringReader(new String(bytes));
	};
}
 
Example 4
Source File: SpringMustacheApplication.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public Mustache.Compiler mustacheCompiler(Mustache.TemplateLoader templateLoader, Environment environment) {

    MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector();
    collector.setEnvironment(environment);

    return Mustache.compiler()
      .defaultValue("Some Default Value")
      .withLoader(templateLoader)
      .withCollector(collector);

}
 
Example 5
Source File: JMustacheViewResolver.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public View getView(String viewName, final ResourceLoader resourceLoader) throws Exception {
	String filename = filename(viewName);

	byte[] bytes = resourceLoader.load(filename);
	if (bytes == null) return null;

	Mustache.TemplateLoader loader = loader(resourceLoader);

	String template = new String(bytes);

	Mustache.Compiler compiler = getViewFactory(resourceLoader);

	Template mustache = compiler.withLoader(loader).compile(template);

	return view(mustache);
}