Fast, Modular, Easy-to-learn

Build complex and modular web pages for your Spring Boot application using the Phoenix Template Engine. You can use the already-familiar Java syntax inside your HTML templates.

Phoenix is open-source, easy to use, and offers many advantages over other template engines.

Get started

The power of Java inside your templates

The `@` character

Only one special character, `@`, is used to distinguish between HTML and Java code. You can easily insert for or if, using only the @ character before them. And many more...

Create modular templates

You can easilly define fragments or templates that can be reused and imported inside other templates. Further more, you can send HTML content blocks as input parameters.

Use Java

Phoenix was designed with Java in mind. You can import any Java class from your code or standard Java classes and use them inside your templates. Furthermore, the default Java syntax is used inside templates, so you don't need to memorise utilities or special syntaxes.

Fast, fast and fast

Phoenix templates are compiled into Java classes. This means that they are fast. Phoenix is still in early development, however it features promising results compared to other popular template engines. The resulting HTML page is quickly assembled and ready to be returned from any controller using the View.of() syntax.

Reverse routing

Phoenix supports reverse routing, meaning that endpoints are determined at runtime by the engine. If an endpoint changes in the controller you don't need to make any changes inside your template. And yes, dynamic URLs are supported.

Easy to integrate and configure

Phoenix can be integrated with any Spring Boot application. Just import the library and it's few dependencies. Configuration is also straight-forward.

Rocker compatible

Phoenix is almost 100% compatible with Rocker template engine (WIP to achieve full-compatibility) while offering more features. Migrate your existing webapps that use Rocker in just a few minutes!

Open Source

Phoenix is fully open-source and free to use, even for commercial projects. Feel free to download the code, contribute, submit a bug or request features!

Easy to learn

Phoenix uses only the @ special character to insert Java code inside your template

Phoenix makes it easy to integrate Java code

Phoenix uses the @ character to insert Java code inside your template. You can use it to write @if or @for statements, as well as print variables with ease

                                    
@if(a == 0) {
    <h6>@b</h6>
}

@if(b == 0) {<h6>AAAA</h6>}

<p>
    Operation: @a + @b = @(a + b)
</p>
@if (a == 1) {
    <div>
        <a href="mailto:test2@@domain.com">Email admin</a>
    </div>
}
@for(int count = 0; count<4; count++) {
    <p>We are counting @count</p>
    @if(count == 3) {
        <p>Reached @count</p>
    }
    <p>Let's see what happens</p>
}
                                    
                                

Define reusable templates

Define reusable components that you can combine to create complex web pages. Send parameters and even content blocks with ease from one template to another. Further more, Phoenix allows you to retrieve components from the client-side using simple GET requests.

fragments/withContent.java.html
                                                
@args(int a, PhoenixContent content)

<h1>Fragment @a</h1>

@content
                                                
                                            
index.java.html
                                                
<h1>Fragment is below</h1>
@fragments.withContent.template(a) {
    <p>This is my text</p>
    <a href="@(a).html">It holds another URL</a>
}
                                                
                                            

Import and use Java classes inside your template

You can send instances of your Java objects from the controller to the HTML template with ease and even use Java static imports

                                        
@import java.util.List;
@import data.dto.Stock;
@import static java.lang.System.currentTimeMillis

@args (List<Stock> items)

<body>
    <div>Current time in millis is @currentTimeMillis()</div>
    <div> The list has @items.size() items</div>
</body>
                                        
                                    

Render HTML or JSON using the Phoenix Result and the Phoenix View

Import the tech.petrepopescu.phoenix.format.Result and tech.petrepopescu.phoenix.format.View in your controller and render JSON or HTML code, as needed. Using the PhoenixResponse.ok() you can send any Java object to render it as a JSON string or send a View to have it rendered as HTML

                                        
@Controller
public class TestController {
    @GetMapping("/test.html")
    public Result renderTest(@RequestParam(name = "a", defaultValue = "0") int a,
                             @RequestParam(name = "b", defaultValue = "0") int b) {
        return ok(View.of("test", a, b));
    }

    @GetMapping("/json")
    public Result renderJson() {
        return ok(Arrays.asList("Test", "test2"));
    }
}
                                        
                                    

Reverse routing for your controllers

Phoenix will calcualte the URLs from your controllers and will provide an easy way of inserting the URLs inside your template. And yes, dinamic URLs are supported, with PathVariable and Requestparam. If the URL of your endpoint changes you won't need to make any changes to your templates.

TestController.java
                                            
@Controller
public class TestController {

    @GetMapping("/test.html")
    public Result renderTest(@RequestParam(name = "a", defaultValue = "0") int a,
                                @RequestParam(name = "b", defaultValue = "0") int b) {
        return ok(View.of("test", a, b));
    }
}
                                            
                                        
main.java.html
                                            
<a href="@routes.TestController.renderTest(0, 3)">
    Go to this page
</a>
                                            
                                        

Some quick benchmarks

Phoenix in still in eary development and things may change in the future, but at the moment it has really good benchmarks. Look at the benchmark page for more details, but Phoenix may be one of the fastest template engines currently available for Spring. In my rudimentary benchmark, Phoenix completed in under 800ms, orders of magnitude faster than Thymeleaf and even slightly faster than Rocker.

Want to support the project?

If you want to support the project in any way, feel free to contact me using the contact form from my blog. Phoenix is still in its early stages and more work is needed to make it production ready, so any support is greatly appreciated

Considering offering financial support? Message me and we can talk about this as well.

Want to use Phoenix for your projects? GREAT! Share with us the link to your project/company and we may feature it.