Skip to content

Commit

Permalink
fix rendering docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tipsy committed Feb 19, 2024
1 parent 96b38dd commit 73ff0d0
Showing 1 changed file with 95 additions and 36 deletions.
131 changes: 95 additions & 36 deletions pages/plugins/rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,37 @@ implementation("io.javalin:javalin-rendering:{{site.javalinversion}}")
</div>
</div>

### Included template engines
The `javalin-rendering` artifact includes default implementations for several template engines:

| --- | --- |
| **Freemarker** | &nbsp;&nbsp; [https://freemarker.apache.org](https://freemarker.apache.org) |
| **JTE** | &nbsp;&nbsp; [https://jte.gg/](https://jte.gg) |
| **Mustache** | &nbsp;&nbsp; [https://github.com/spullara/mustache.java](https://github.com/spullara/mustache.java) |
| **Pebble** | &nbsp;&nbsp; [https://pebbletemplates.io/](https://pebbletemplates.io) |
| **Thymeleaf** | &nbsp;&nbsp; [https://www.thymeleaf.org/](https://www.thymeleaf.org) |
| **Velocity** | &nbsp;&nbsp; [https://velocity.apache.org/](https://velocity.apache.org) |
| **Commonmark** | &nbsp;&nbsp; [https://github.com/commonmark/commonmark-java](https://github.com/commonmark/commonmark-java) |

## Using the plugin
All the template engines look for templates/markdown files in `src/resources/templates`.
To enable a template engine, you have to register it on the Javalin config:

All the template engines look for templates/markdown files in `src/resources/templates`,
and the correct rendering engine is chosen based on the extension of your template.
Javalin will automatically initialize the template engine of an included dependency
for you, but you can also initialize it yourself (if you want to configure it).
{% capture java %}
Javalin.create(config -> {
config.fileRenderer(new JavalinMustache());
});
{% endcapture %}
{% capture kotlin %}
Javalin.create { config ->
config.fileRenderer(JavalinMustache())
}
{% endcapture %}
{% include macros/docsSnippet.html java=java kotlin=kotlin %}

You can also register your own rendering engine.

### Rendering a template

Once you have added the dependencies for the template engine you want to use,
all you have to do is call `ctx.render()` with the path to your template file (and optionally a model):
{% capture java %}
Expand All @@ -65,44 +85,83 @@ ctx.render("/templateFile.ext", mapOf("firstName" to "John", "lastName" to "Doe"

### Configuring a template engine
If you wish to configure a template engine (for example, to set a root directory for your template files),
all `init` methods receive optional parameters with their template engine
configurations:

```kotlin
JavalinFreemarker.init(configuration: Configuration?)
JavalinJte.init(templateEngine: TemplateEngine?, isDevFunction: ((Context) -> Boolean)?)
JavalinMustache.init(mustacheFactory: MustacheFactory?)
JavalinPebble.init(pebbleEngine: PebbleEngine?)
JavalinThymeleaf.init(templateEngine: TemplateEngine?)
JavalinVelocity.init(velocityEngine: VelocityEngine?)
JavalinCommonmark.init(htmlRenderer: HtmlRenderer?, parser: Parser?)
```

### Registering a new engine:
{% capture java %}
JavalinRenderer.register(new JavalinPebble(), ".peb", ".pebble");
all constructors have optional parameters with their template engine configurations:

JavalinRenderer.register((filePath, model, ctx) -> {
return MyRenderer.render(filePath, model, ctx);
}, ".ext");
{% capture java %}
Javalin.create(config -> {
config.fileRenderer(new JavalinVelocity(myVelocityEngine));
});
{% endcapture %}

{% capture kotlin %}
JavalinRenderer.register(JavalinPebble(), ".peb", ".pebble")

JavalinRenderer.register({ filePath, model, ctx ->
MyRenderer.render(filePath, model, ctx)
}, ".ext")
Javalin.create { config ->
config.fileRenderer((JavalinVelocity(myVelocityEngine)
}
{% endcapture %}
{% include macros/docsSnippet.html java=java kotlin=kotlin %}

### Good to know
The configuration classes are not from Javalin, but from the template engine you are using,
so please consult the documentation for that particular template engine to learn how to use them.

## Good to know
Please consult the documentation for that particular template engine to learn how to use
them, these kinds of settings are not handled through Javalin.

If you need to configure settings beyond what's available in `JavalinTemplateEngine.init` (for example,
to set a custom file extension), you have to write your own implementation and register it using
`JavalinRenderer.register`.
## Recreating the old JavalinRenderer
Older versions of Javalin had a `JavalinRenderer` class that was used to render templates.
This class was able to render templates based on the file extension.

You can recreate this class like this:

Note that if you're using `JavalinRenderer`, these are global settings,
and cannot be configured per instance of Javalin.
{% capture java %}
class JavalinRenderer implements FileRenderer {
private Map<String, FileRenderer> renderers = new HashMap<>();
public JavalinRenderer register(String extension, FileRenderer renderer) {
renderers.put(extension, renderer);
return this;
}

@Override
public String render(String filePath, Map<String, ? extends Object> model, Context context) {
String extension = filePath.substring(filePath.lastIndexOf(".") + 1);
return renderers.get(extension).render(filePath, model, context);
}
}
{% endcapture %}
{% capture kotlin %}
class JavalinRenderer : FileRenderer {
private val renderers = HashMap<String, FileRenderer>()
fun register(extension: String, renderer: FileRenderer): JavalinRenderer {
renderers[extension] = renderer
return this
}


override fun render(filePath: String, model: Map<String, Any?>, context: Context): String {
val extension = filePath.substring(filePath.lastIndexOf(".") + 1)
return renderers[extension]!!.render(filePath, model, context)
}
}
{% endcapture %}
{% include macros/docsSnippet.html java=java kotlin=kotlin %}

You can then register it like any other renderer:

{% capture java %}
Javalin.create(config -> {
config.fileRenderer(
new JavalinRenderer()
.register("mustache", new JavalinMustache())
.register("jte", new JavalinJte())
);
});
{% endcapture %}
{% capture kotlin %}
Javalin.create { config ->
config.fileRenderer(
JavalinRenderer()
.register("mustache", JavalinMustache())
.register("jte", JavalinJte())
)
}
{% endcapture %}
{% include macros/docsSnippet.html java=java kotlin=kotlin %}

0 comments on commit 73ff0d0

Please sign in to comment.