Cedilla, The Fastest Smallest Java Template Engine
A template engine can be seen as a sort of printf, of primordial origin from the C standard library, on steroid. However too often 'on steroids' means either that there is a lot of things to learn before becoming proficient in the use of a new framework, or that dependencies hell increases, or even, as often happens, both of the two occurs. Cedilla is a new java template engine with a really unusual twist that avoids both problems in a very surprising way.
The template engine presented in this article is open source software, published on GitHub under the commercially friendly Apache License 2.0. It has been written by SyntaxCorrect developers and is used for example in the development of this website too. The information provided here is therefore biased, of course, and we think it's our duty to warn you about it before you read the article. On the other hand, if we created this template engine it's because we think it offers advantages that are not found in other engines and the purpose of this article is exactly to introduce them to you. After all, this library of ours is freely available for everyone to try out, use, fork, or whatever one wants.
Note: The template engine presented in this article is open source software, published on GitHub under the commercially friendly Apache License 2.0. It has been written by SyntaxCorrect developers and is used for example in the development of this website too. The information provided here is therefore biased, of course, and we think it's our duty to warn you about it before you read the article. On the other hand, if we created this template engine it's because we think it offers advantages that are not found in other engines and the purpose of this article is exactly to introduce them to you. After all, this library of ours is freely available for everyone to try out, use, fork, or whatever one wants.
Java has a number of template engines, most of which are very good, free and not too cumbersome to deploy. From the Java official JSP specifications and its various open source implementations, to the well known and widely adopted Apache FreeMarker, Apache Velocity, as well as Thymeleaf, one could wonder why we felt the need of a new template engine at the point to develop one ourselves instead of quickly adopting one of the well established solutions.
Well, there are many reasons, as we are going to see, but one above all immediately won us when we realized it: if to use a template engine you need more time to learn it than developing one on your own which is way more powerful, faster and smaller, then go with developing one on your own.
This is in fact the #1 goal of Cedilla: you don't need to learn anything to use all Cedilla extremely powerfull features, constructs, loops, extensions and potentially hundred of thousands of additional libraries.
Why? Well, take a look at the "Hello Cedilla World!" template:
Probably yes, but even if not, don't worry: there are only two things to learn to immediately use Cedilla. Let's see what they are.
Cedilla, or how to not reinvent the wheel
In a Cedilla template:
§ var foo = "Cedilla"
- JSP has been designed specifically for HTML pages. Cedilla does not have this limit, you could even employ it to template binary files if you want, and surely any other kind of text file.
- JSP forces you to adopt the whole servlet/container specification of Java. This is for example one of the main reasons proponents of alternatives technologies often prefer NodeJs or Ruby and so on to Java when developing dynamic websites. Well the point is: you don't need to be locked in any legacy framework in order to use Cedilla. This very same website, SyntaxCorrect is entirely written in Java but not one single line of code uses neither Spring, Java EE, Containers of any kind or whatever framework/specification you could imagine. Java can be extremely quick and way more lightweight than any of the other supposedly easier alternatives if you know how to program it. Cedilla was born with exactly those goals in mid: to be fast, very small, no dependencies on external specifications/frameworks and require nothing to learn, or even just read, to be immediately mastered.
Cedilla, there is more under the hood
So is Cedilla just that two things to fully use it? Yes, in the sense that with those two things you can make whatever you want. At the same time we soon discovered that some of the things that most often we would have liked to do could be officially, even if optionally, supported directly by our small library, so we implemented them as core functionalities.
Normally you would write the code:
To get the output:
You can get the same result passing the value for **foo** in a variety of ways, for example in an Map:
Of course you can load your template from a file:
FileSystemExtras and many others, are part of our core library included in the Cedilla distribution, but of course you can load files and create Maps the way you want, you're not forced to use our core library even if suggested: they contain a lot of usefull extra classes most of which are self-explanatory.
CedillaHelper which just need to implement one single function:
Yes, some sugar syntax is provided too
While strictly not necessary at all, there are a couple sugar syntax shortcut you could like to use with Cedilla. We added this expecially for those like us who were used to other template engines.
Expressions are allowed too, you include them in §= <expression> §:
The very small and yet full list of possibilities is present on the official repository on GitHub:
Enjoy Cedilla, free Apache License 2.0 software for you to try, fork, extend, or just use it for all your projects that need templates.