• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

casid/jte: Secure and speedy templates for Java and Kotlin.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

casid/jte

开源软件地址(OpenSource Url):

https://github.com/casid/jte

开源编程语言(OpenSource Language):

Java 98.5%

开源软件介绍(OpenSource Introduction):

jtejte is a secure and lightweight template engine for Java and Kotlin. 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

  • Intuitive and easy syntax, you'll rarely need to check the documentation
  • Write plain Java or Kotlin for expressions
  • Context-sensitive HTML escaping at compile time
  • IntelliJ plugin with completion and refactoring support
  • Hot reloading of templates during development
  • Blazing fast execution (see benchmarks)

TLDR

jte gives you the same productive, type safe experience you're used to from writing Java or Kotlin. 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.
  • ${} 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.

If you had more than one page like example.jte, you would have to duplicate a lot of shared template code. Let's extract the shared code into another template, so that it can be reused.

Let's move stuff from our example page to layout.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 template:

@import org.example.Page

@param Page page

@template.layout(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 AMD Ryzen 5950x (single thread):

alt Template Benchmark

High concurrency

This is the same benchmark as above, but the amount of threads was set to @Threads(16), to fully utilize all cores. jte has pretty much zero serialization bottlenecks and runs very concurrent on servers with a lot of CPU cores: alt Template Benchmark

Getting started

jte is available on Maven Central:

Maven

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

Gradle

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

No further dependencies required! Check out the syntax documentation and have fun with jte.

Framework integration

Websites rendered with jte




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap