Introduction
Let me introduce you to Tuxedo, a template engine written in Swift.
Why?
Swift is a relatively young language, and there is no existing, comprehensive-enough templating library available for the Swift language and platform. This was a key motivator for the author to take the effort and build something awesome that fills this gap for the platform, and be able to use it in many different use-cases.
It's also worth highlighting that server-side Swift projects become more and more popular every day, and HTML templates are the most widely used areas for template engines on other platforms. Tuxedo was designed primarily with this use-case in mind.
Key personal motivators
- Learn a completely new area of software development. Creating compilers and interpreters are profoundly different from app development
- Create something useful that everyone can use in different use-cases
- The author had a code-generation project, which used a template engine to generate Objective-C and Swift model files. The goal is to convert that project into a Swift project without any other third party template language dependencies
How?
Putting a lot of thoughts into how interpreters work, the author took the approch to create an abstraction layer first, and then implement the templating language as one of the concrete use-cases of the interpreter. So as a first step, implementing the Eval framework (which is a lightweight and generic interpreter) opened up a way to easily create a new template language.
Eval is a lightweight and generic interpreter that recognises patterns and turns them into expressions. Tuxedo uses Eval and brings template-specific implementation features on top of it, such as helpers for operator and function definitions. It also comes with a set of standard library elements that uses these helpers to effectively create new and new language features into Tuxedo. Check out the API reference to learn more.
What?
It is a template engine written in Swift, really similar to other template languages from different programming environments, such as Django, Synth, Twig, Smarty, Nunjucks, Grantlee, or Liquid.
It allows you to separate the UI and rendering layer of your application from the business logic. Smart templates working with raw data allow the frontend to be handled and developed separately from other parts of the application, so processing, layouting and formatting your output can be defined in very simple template formats.
Why the name? It dresses up your static output with elegant dynamic templates, using control statements, and high level operators.
The project was built upon the Eval framework, a lightweight and generic interpreter that recognises patterns and turns them into expressions. For a short time, Tuxedo served as an example application of what is possible using this evaluator. Soon, the template language example turned out to be a really useful project on its own, so I extracted it to live as a separate library and be used by as many projects as possible. I see the possibility of applications most valuable especially in server-side Swift projects, but there are a lot of other areas where template parsing fits well.