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.
Introduction
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:
Seems familiar?
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:
- whatever follows a '§ ' (which is the char § followed by a space char) is JavaScript code till the end of the line. In the above example this is the line
§ var foo = "Cedilla"
- whatever follows a '§', without any space char following it (like in §foo), is replaced with the content of a JavaScript variable you defined elsewhere in the template (like in the 'Hello Cedilla World' example above) or you passed as an argument to the template engine.
That's all you need to know to be super-uber-iper productive since instant zero with Cedilla template engine. Can you envision why? Easy, if you know JavaScript, you already know how to implement loops, ifs, switches, do...while, etc. Cedilla scripting is the same pure, plain JavaScript that you already know and use everyday. There is nothing new to learn here, move on, move to the next step: enjoying its power.
Cedilla was born from the desire not to reinvent the wheel, and in this case specifically because by reinventing it you also have to learn, or teach others, how to use it. This is a problem that affects all of the other Java template engines. Go on their respective websites, if you see their documentation, FreeMarker, Velocity, Thymeleaf, everyone of them requires substantial amount of time even just to read the whole documentation. In the beginning we tried to employ one of them, but we found that for every construct, even a simple loop to show the records from a database, we had first to learn many specifics of the template engine. With Cedilla this doesn’t happen because if you know how to write a loop in JavaScript, then, well, you know how to write it in Cedilla too.
Nowaday every programmer knows that, if you are a developer, you should know some languages if you want to stay on the market. For sure you need to know Java, Python, some C/C++, absolutely you need to know JavaScript. Do you really need to learn another language, and a very domain-specific one, just for a template engine? We believe no, and we are not alone. In fact JSP also was born with a similar goal. Inspired by the first ever web template engine, which was PHP, JSP was born to allow programmers to write HTML pages with a mixture of HTML and Java itself as a scripting language. While absolutely a wonderful idea, however, JSP presents many drawbacks which in the end resulted in the proliferation of the alternative template engines we have today. The main problems with JSP are all problems that Cedilla does not have:
- 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.
- Java is a wonderful language and we surely love it, but templating is way better handled by a scripting language like JavaScript. Just to mention a key difference between programming and scripting: in a programming language you want a static type system, really, anyone who has been programming professionally for more than a few months knows this. In a scripting language, however, having types is just heavy and cumbersome.
- 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.
On the other side both FreeMarker, Velocity and Thymeleaf do not suffer any of the JSP's problems. But unfortunately they have a very steep learning curve and sorry but no one of them is so powerfull as a JavaScript engine cna be. In fact one of the other reason Cedilla does not invent any wheel is exactly because internally, Cedilla uses the Java Nashorn built-in JavaScript engine to render templates. There is already all the needed power inside Java for templating, let's use it.
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:
Note that StringExtras
, 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.
A part from this basic usages Cedilla gives you the chance to implement modularity on your own. Inside a Cedilla template you can use the predefined JavaScript functions loadFile
and importFrom
. The former is used to load an external template inside the current one, the latter is used to load external JavaScript files, such as libraries and JavaScript code in general. The good part of this functions is that it's you who define how external files are loaded. Infact you pass Cedilla a class that implements the interface CedillaHelper
which just need to implement one single function: String getSourceFor(String path);
. This way you are given max freedom on how to recover your nested template files and JavaScript libraries.
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.
You can close a line of JavaScript and continue with the template on the same line. For example this is valid:
In addition you can insert JavaScript code on more lines using §| <javascript> |§ instead of § <javascript> §:
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:
Conclusion
A template engine can bring you a long way if it is small, fast, does not lock you in any other framework and you need to learn absolutely nothing at all to use it immediately. Not only will you discover soon that templating your webpages becomes quick and fun again, you’ll automatically find many other uses for it. In fact, if inside a template engine you have the full power of one of the most powerful scripting languages ever created, JavaScript, you’ll surely find yourself wondering: why, oh why did I not get this before?
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.