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.
`@`
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...
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.
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.
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.
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.
Phoenix can be integrated with any Spring Boot application. Just import the library and it's few dependencies. Configuration is also straight-forward.
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!
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!
@
special character to insert Java code inside
your templatePhoenix 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 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.
@args(int a, PhoenixContent content)
<h1>Fragment @a</h1>
@content
<h1>Fragment is below</h1>
@fragments.withContent.template(a) {
<p>This is my text</p>
<a href="@(a).html">It holds another URL</a>
}
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>
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"));
}
}
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.
@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));
}
}
<a href="@routes.TestController.renderTest(0, 3)">
Go to this page
</a>
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.
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.