All Projects → casid → Jte

casid / Jte

Licence: apache-2.0
jte is a secure and lightweight template engine for Java.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Jte

Manifold
Manifold plugs into Java to supplement it with powerful features, from Type-safe Metaprogramming (direct access to GraphQL, JSON, XML, etc.), Extension Methods, Operator Overloading, and Unit Expressions to an integrated Template Engine and a Preprocessor. All fully supported in IntelliJ IDEA and Android Studio. Simply add Manifold to your project and begin taking advantage of it.
Stars: ✭ 993 (+335.53%)
Mutual labels:  intellij, template-engine
Intellij Sdk Code Samples
Mirror of the IntelliJ SDK Docs Code Samples
Stars: ✭ 217 (-4.82%)
Mutual labels:  intellij
Intellij Haxe
Haxe plugin for IntelliJ Platform based IDEs (IDEA, Android-Studio)
Stars: ✭ 188 (-17.54%)
Mutual labels:  intellij
Ructe
Rust Compiled Templates with static-file handling
Stars: ✭ 206 (-9.65%)
Mutual labels:  template-engine
Yarte
Yarte stands for Yet Another Rust Template Engine
Stars: ✭ 189 (-17.11%)
Mutual labels:  template-engine
Idea Php Annotation Plugin
Add PHP annotation support for PhpStorm and IntelliJ
Stars: ✭ 216 (-5.26%)
Mutual labels:  intellij
Deeplearning4j Examples
Deeplearning4j Examples (DL4J, DL4J Spark, DataVec)
Stars: ✭ 2,215 (+871.49%)
Mutual labels:  intellij
Protobuf Jetbrains Plugin
Protobuf Support for JetBrains IDEs
Stars: ✭ 226 (-0.88%)
Mutual labels:  intellij
Intellij Key Promoter X
Modern IntelliJ plugin to learn shortcuts for buttons
Stars: ✭ 2,689 (+1079.39%)
Mutual labels:  intellij
Intellij Hcl
HCL language support for IntelliJ platform based IDEs
Stars: ✭ 207 (-9.21%)
Mutual labels:  intellij
Idea Live Templates
My IntelliJ Live Templates
Stars: ✭ 207 (-9.21%)
Mutual labels:  intellij
Intellij Csv Validator
CSV validator, highlighter and formatter plugin for JetBrains Intellij IDEA, PyCharm, WebStorm, ...
Stars: ✭ 198 (-13.16%)
Mutual labels:  intellij
Hyperium
Hyperium, Free Minecraft client with HUDs and Popular mods
Stars: ✭ 217 (-4.82%)
Mutual labels:  intellij
Elefant
Elefant, the refreshingly simple PHP CMS and web framework.
Stars: ✭ 188 (-17.54%)
Mutual labels:  template-engine
Flexml
🚀基于Litho的Android高性能动态业务容器。
Stars: ✭ 225 (-1.32%)
Mutual labels:  template-engine
Thymeleaf
Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
Stars: ✭ 2,251 (+887.28%)
Mutual labels:  template-engine
Intellij Emberjs
Ember.js support for JetBrains IDEs (IntelliJ, WebStorm, ...)
Stars: ✭ 202 (-11.4%)
Mutual labels:  intellij
Dotfiles
🐧 Simple, fast, productivity-increaser dotfiles
Stars: ✭ 213 (-6.58%)
Mutual labels:  intellij
Osx Ramdisk
RAM Disk creator in OS-X for IntelliJ, Google Chrome and other apps cache to make them fly.
Stars: ✭ 229 (+0.44%)
Mutual labels:  intellij
Java Oo
Java Operator Overloading
Stars: ✭ 226 (-0.88%)
Mutual labels:  intellij

jtejte is a secure and lightweight template engine for Java. All jte templates are compiled to Java class files, meaning jte adds essentially zero overhead to your application. jte is designed to introduce as few new keywords as possible and builds upon existing Java features, so that it is very easy to reason about what a template does. The IntelliJ plugin offers full completion and refactoring support for Java parts as well as for jte keywords. Supports Java 8 or higher.

Build Status Coverage Status License Maven Central

Features

TLDR

jte is a lot of fun to work with! This is IntelliJ with the jte plugin installed:

jte in IntelliJ

5 minutes example

Here is a small jte template example.jte:

@import org.example.Page

@param Page page

<head>
    @if(page.getDescription() != null)
        <meta name="description" content="${page.getDescription()}">
    @endif
    <title>${page.getTitle()}</title>
</head>
<body>
    <h1>${page.getTitle()}</h1>
    <p>Welcome to my example page!</p>
</body>

So what is going on here?

  • @import directly translates to Java imports, in this case so that org.example.Page is known to the template.
  • @param Page page is the parameter that needs to be passed to this template.
  • @if/@endif is an if-block. The stuff inside the braces (page.getDescription() != null) is plain Java code. @JSP users: Yes, there is @elseif() and @else in jte ❤️.
  • ${} writes to the underlying template output, as known from various other template engines.

To render this template, an instance of TemplateEngine is required. Typically you create it once per application (it is safe to share the engine between threads):

CodeResolver codeResolver = new DirectoryCodeResolver(Path.of("jte")); // This is the directory where your .jte files are located.
TemplateEngine templateEngine = TemplateEngine.create(codeResolver, ContentType.Html); // Two choices: Plain or Html

The content type passed to the engine determines how user output will be escaped. If you render HTML files, Html is highly recommended. This enables the engine to analyze HTML templates at compile time and perform context sensitive output escaping of user data, to prevent you from XSS attacks.

With the TemplateEngine ready, templates are rendered like this:

TemplateOutput output = new StringOutput();
templateEngine.render("example.jte", page, output);
System.out.println(output);

Besides StringOutput, there are several other TemplateOutput implementations you can use, or create your own if required.

example.jte works, but imagine you have more than one page. You would have to duplicate a lot of shared template code. Let's extract the shared code into a tag. Tags are template snippets that can be called by other templates.

All tags must be created in a directory called tag in your template root directory.

Let's move stuff from our example page to tag/page.jte:

@import org.example.Page
@import gg.jte.Content

@param Page page
@param Content content

<head>
    @if(page.getDescription() != null)
        <meta name="description" content="${page.getDescription()}">
    @endif
    <title>${page.getTitle()}</title>
</head>
<body>
    <h1>${page.getTitle()}</h1>
    ${content}
</body>

The @param Content content is a content block that can be provided by callers of the template. ${content} renders this content block. Let's refactor example.jte to use the new tag:

@import org.example.Page

@param Page page

@tag.page(page = page, content = @`
    <p>Welcome to my example page!</p>
`)

The shorthand to create content blocks within jte templates is an @followed by two backticks. For advanced stuff, you can even create Java methods that return custom Content implementation and call it from your template code!

Check out the syntax documentation, for a more comprehensive introduction.

Performance

By design, jte provides very fast output. This is a fork of mbosecke/template-benchmark with jte included, running on a MacBook Pro 2015 (single thread):

alt Template Benchmark

Note that the above is with ContentType.Plain, so no output escaping is done. This is basically what the other engines in the benchmark are set-up with. Well, except Thymeleaf I think. Since jte 0.8.0, you will want to render HTML pages with ContentType.Html, so that output is automatically escaped by the engine, depending on where in the HTML data is written to. With ContentType.Html, jte is still extremly fast, thanks to owasp-java-encoder:

alt Template Benchmark

High concurrency

This is a fork of mbosecke/template-benchmark with jte included, running on an AMD Ryzen 5950x:

alt Template Benchmark

For this benchmark, the amount of threads was manually set @Threads(32), to fully utilize all cores. jte has pretty much zero serialization bottlenecks and runs very concurrent on servers with a lot of CPU cores.

Getting started

jte is available on Maven Central:

Maven

<dependency>
    <groupId>gg.jte</groupId>
    <artifactId>jte</artifactId>
    <version>1.9.0</version>
</dependency>

Gradle

implementation group: 'gg.jte', name: 'jte', version: '1.9.0'

No further dependencies required! Check out the syntax documentation and start hacking :-)

Framework integration

Websites rendered with jte

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].