I'm reading through the docs and looking online and struggling to use any of the templating engines like markdown or freemarker. I do not have a renderThymeLeaf or similar functions available on my context object. How is this supposed to be configured?
I'm reading the unit tests right now. Do I actually need to do anything to get the interpolation to work?
It should just be render(path, model). The suffixes were removed in 2.0, it finds the correct engine based on the file extension. Where did you find the outdated example?
Hm, must have been linked on outdated docs somehow. Is it possible (if I'm not doing templating) to just return a standard html file from the resources dir?
Yes, you can use a static resource handler for that: https://javalin.io/documentation#static-files
@tipsy I'm ultimately just looking to do something along the lines of context.render("file.html");
That seems a bit odd, but you can register a .html FileRenderer which just reads the file:
JavalinRenderer.register(FileRenderer { path, model ->
String(Files.readAllBytes(Paths.get(path)))
}, ".html")
hmm. I must be doing something wrong, I have the following:
JavalinRenderer.register(
(String path, Map<String, Object> model) -> {
try {
return new String(Files.readAllBytes(Paths.get(path)));
} catch (IOException e) {
return null;
}
},
".html");
app.get("/", redirectHandler);
then
context.render("AuthenticationSuccess.html");
or
context.render("AuthenticationSuccess.html", model("test", "test"));
Yields:
Uncaught exception
java.lang.IllegalStateException: renderer.render(filePath, model) must not be null
Sorry, I must be missing something obvious.
You're swallowing an exception and returning null. The render method is not allowed to return null. Do e.printStackTrace() instead, and you'll find out what the problem is.
@Schachte here is a full example:
package io.javalin.examples;
import io.javalin.Javalin;
import io.javalin.rendering.JavalinRenderer;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
public class HtmlExample {
static {
JavalinRenderer.register(HtmlExample::readFile, ".html");
}
public static void main(String[] args) {
Javalin app = Javalin.create().start(7070);
app.get("/my-path", ctx -> ctx.render("src/test/external/html.html"));
}
private static String readFile(String path, Map<String, Object> ignored) {
try {
return new String(Files.readAllBytes(Paths.get(path)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
I'm guessing you specified a path which resulted in a IOException.
This still sounds like a strange thing to want to do though, could you tell me a bit about your use-case ?
@tipsy Thanks for the example! I'll give it a shot. Essentially I'm doing an OAuth flow. The redirect URI from the OAuth provider hits my backend, which proxies a request to another third party. I want to conditionally return to the client some HTML from the server-side to let them know if the token authentication was successful. Because the redirect happens from an external source, this is the cleanest way I could think of to conditionally render HTML to the client to let them know if their auth was successful or not.
If it's a one off thing it might be simpler to just set the result directly:
app.get("/", ctx -> ctx.html(new String(Files.readAllBytes(Paths.get("/path/to/file.html"))));
Note to self: This comes up a surprising amount of times, maybe I should add readFile to FileUtil 馃
ah, honestly, I like this solution better for my case. The handler itself does a ton of processing, but I just want to conditionally return the html of 2 different files conditionally. This looks exactly like what I needed. Currently I am calling ctx.html(MASSIVE_HTML_STRING_HERE) and it really clutters my class.
Thanks a lot for the help. I'll give this latter option a shot right now.
You're welcome! It does sound like a better fit.
Very off topic, but I just visited your website and watched "Introduction To Docker and Docker Containers". Your style and the overall quality of the video is great! Would you be interested in creating a Javalin video?
@tipsy Thanks! Actually sounds like a cool idea. I'll do that next!
Great, happy to hear that! I've created an issue: https://github.com/tipsy/javalin/issues/549
I'll add some info there.
Most helpful comment
@tipsy Thanks! Actually sounds like a cool idea. I'll do that next!