Java Code Examples for com.github.mustachejava.Mustache

The following are top voted examples for showing how to use com.github.mustachejava.Mustache. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: Foodoo-backend   File: Templates.java View source code 11 votes vote down vote up
public static String getTestTemplate(String name, String status, String date) {
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache template = mf.compile("test.mustache");

    StringWriter writer = new StringWriter();
    HashMap<String, String> scopes = new HashMap<>();
    scopes.put("catname", name);
    scopes.put("status", status);
    scopes.put("date", date);

    template.execute(writer, scopes);

    return writer.toString();
}
 
Example 2
Project: essentials   File: TemplateUtils.java View source code 9 votes vote down vote up
public static String replaceStringPlaceholders(final String content, final Map<String, String> data) {
        if (Strings.isNullOrEmpty(content)) {
            return content;
        }

        try {
            final Writer writer = new StringWriter();
            final MustacheFactory mf = new DefaultMustacheFactory();

            final StringReader reader = new StringReader(content);
            final Mustache mustache = mf.compile(reader, content);

            mustache.execute(writer, data);
            writer.flush();

            return writer.toString();
        } catch (IOException e) {
            log.error("Error flushing template", e);
        }
        return content;

}
 
Example 3
Project: micra   File: MicraServlet.java View source code 6 votes vote down vote up
protected String Mustache (String template, Map scopes)
{
    StringWriter writer = new StringWriter();
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(new StringReader(template), template);
    mustache.execute(writer, scopes);
    writer.flush();

    return writer.toString();
}
 
Example 4
Project: undertow.js   File: MustacheTemplateProvider.java View source code 6 votes vote down vote up
@Override
public Template getTemplate(String templateName) {
    try {
        MustacheFactory mf = new DefaultMustacheFactory();
        final Mustache mustache = mf.compile(new StringReader(Templates.loadTemplate(templateName, resourceManager)), templateName);

        return new Template() {
            @Override
            public String apply(Object data) {
                final StringWriter stringWriter = new StringWriter();
                mustache.execute(stringWriter, data);
                return stringWriter.getBuffer().toString();
            }
        };
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Project: torrenttunes-client   File: WriteMultilingualHTMLFiles.java View source code 6 votes vote down vote up
private static void writeForLanguageMap(Strings language, String outputFile) {
	try {
	
		/*
		 * There are unfortunately two layers of mustache. The first is the strings,
		 * {{strings.blah}}, and the second is within the scripts, like {{artist.mbid}}
		 * 
		 * Your main.template file changed all the {{artist.mbid}} to @@artist.mbid~~ , 
		 * because otherwise this first language pass will fuck up all the others too,
		 * so after this first pass, you change the back to {{artist.mbid}}
		 * 
		 * { to @
		 * } to ~
		 */
		
		
		Reader reader = new FileReader(new File(DataSources.HTML_TEMPLATE_LOCATION()));

		File file = new File(outputFile);
		if (!file.exists()) file.createNewFile();
			
		Writer writer = new FileWriter(file);
		
		MustacheFactory mf = new DefaultMustacheFactory();
		Mustache mustache = mf.compile(reader, "example");
		mustache.execute(writer, language.map);
		writer.flush();
		
		// Now replace all leftover mustache back to correct {{artist.mbid}}
		String text = Tools.readFile(outputFile);
		text = text.replaceAll("@", "{").replaceAll("~", "}");
		
		Tools.writeFile(text, outputFile);
		
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 6
Project: springyRecords   File: BaseClassWriter.java View source code 6 votes vote down vote up
public void writeCode(File sourceFile, Mustache template, BaseTool tableTool) {
    try {
        logger.info("Creating source "+sourceFile);
        Writer writer = new FileWriter(sourceFile);
        template.execute(writer, tableTool);
        writer.flush();
    }
    catch(Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 7
Project: titanite   File: MustacheViewRenderer.java View source code 6 votes vote down vote up
private BodyWriter render(Mustache mustache, Object model) throws IOException {
    return (out) -> {
        try (OutputStreamWriter writer = new OutputStreamWriter(out)) {
            mustache.execute(writer, model instanceof String ? EMPTY_MODEL : new Object[]{model});
        }
    };
}
 
Example 8
Project: struts2.mustache.java   File: MustacheResult.java View source code 6 votes vote down vote up
@SuppressWarnings("unused")
@Override
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {

	HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
	ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT);

	PrintWriter writer = response.getWriter();

	// Open a stream to read the template passed to the action
	InputStreamReader reader = new FileReader(servletContext.getRealPath(finalLocation));

	// create the resource root
	String resourceRoot = null;

	if (rootMustachePath != null) {
		resourceRoot = rootMustachePath;
	} else {
		resourceRoot = servletContext.getRealPath("/");
	}

	if (reader == null) {
		log.warn("resource at location [" + finalLocation.toString() + "] cannot be obtained (return null) from ServletContext !!! ");
	}
	else {

		response.setContentType("text/html");

		// We need to pass the real path of the templates to the Mustache compiler, in order to support nested templates
		MustacheFactory mf = new DefaultMustacheFactory(new File(resourceRoot));
		Mustache mustache = mf.compile(reader, "mustacheResult");

		mustache.execute(writer, invocation.getAction());

		reader.close();
	}

	writer.flush();
	writer.close();

}
 
Example 9
Project: olca-modules   File: HtmlSerializer.java View source code 6 votes vote down vote up
public String serialize(RootEntity entity, IDatabase database) {
	if (entity == null)
		return null;
	String json = Document.toJson(entity, database);
	String template = getTemplate(entity);
	if (template == null)
		return null;
	MustacheFactory mf = new DefaultMustacheFactory();
	Mustache mustache = mf.compile(template);
	Map<String, Object> content = new HashMap<>();
	content.put("model", entity);
	content.put("json", json);
	StringWriter writer = new StringWriter();
	mustache.execute(writer, content);
	writer.flush();
	return writer.toString();
}
 
Example 10
Project: MIFOSX   File: TemplateMergeService.java View source code 6 votes vote down vote up
public String compile(final Template template, final Map<String, Object> scopes) throws MalformedURLException, IOException {

        this.scopes = scopes;
        this.scopes.put("static", new TemplateFunctions());

        final MustacheFactory mf = new DefaultMustacheFactory();
        final Mustache mustache = mf.compile(new StringReader(template.getText()), template.getName());

        final Map<String, Object> mappers = getCompiledMapFromMappers(template.getMappersAsMap());
        this.scopes.putAll(mappers);

        final StringWriter stringWriter = new StringWriter();
        mustache.execute(stringWriter, this.scopes);

        return stringWriter.toString();
    }
 
Example 11
Project: mustache.java   File: BundleFunctionsTest.java View source code 6 votes vote down vote up
@Test
public void testPreLabels() throws MustacheException, IOException, ExecutionException, InterruptedException {
    MustacheFactory c = new DefaultMustacheFactory(root);
    Mustache m = c.compile("bundles.html");
    StringWriter sw = new StringWriter();
    Map<String, Object> scope = new HashMap<>();
    scope.put("trans", BundleFunctions.newPreTranslate(BUNDLE, Locale.US));
    scope.put("replaceMe", "replaced");
    m.execute(sw, scope);
    assertEquals(getContents(root, "bundles_pre_labels.txt"), sw.toString());
}