Closed enthus1ast closed 2 years ago
Can you reduce it a bit? I'm sure Miran will complain :stuck_out_tongue:
Yeah, the template examples bloat this post a little :/
@narimiran would you complain?
@narimiran would you complain?
Seeing the length of this, I think it needs some changing. But not in the direction you expect :)
Do not reduce it. Show us even more details, and then — this is not anymore an entry for This Month with Nim, it becomes a full guest-post on its own.
@narimiran this would be awesome! Should i change it here inplace or put it somewhere else?
Should i change it here inplace or put it somewhere else?
If you want, you can change it here and ping me once it is ready, or you can immediately do what I would then do in the next step: Fork nim-lang/website and then create a PR there.
Here are the two examples of recent guest posts:
@beef331 im working on the guest post. Wanted to do some changes first.
Name: Nimja
Author: David Krause (enthus1ast)
Nimja
Nimja is a compiled, type safe and fast templating engine written in Nim. It looks like python's
jinja2
or PHP'stwig
, but in contrast to them, it is fully compiled and type safe, since it compiles down to Nim. All the heavy lifting is done on compile time, and the resulting binary contains idiomatic Nim that is very fast on runtime.Since Nimja transforms the templates to Nim on compile time, most Nim code is valid in the templates.
For example:
Nimja not only supports all the basics of a template engine:
if, for, while etc.
but also advanced features like:
extend or white space control.
Extend
Extend in particular is very useful for building websites. It basically allows a child template to choose its outer template.
This way you could just render a detail page, and have it automatically render its surrounding boilerplate.
For example:
master.html
detail.html
When the detail page is rendered in your application, also the master.html is rendered implicitly:
multiple level of extend works as you would expect it.
Imports
Another useful feature of Nimja are the imports. This allows to import a template into another (also multiple times). So you can create small building blocks to use all over your website.
user.html
detail.html
Very Fast
Nimja is not explicitly optimized for performance, but is has some small optimization passes, for example it tries to minimize the number of string concatenations by combining them. Also the way Nimja works (mostly on compile time), makes it very fast.
Ajusa has made a simple template engine benchmark, and I'm very proud that Nimja is that fast:
The meaningfulness of such benchmarks are of course worth discussing, but it shows that you must not sacrifice runtime performance for the joy and ease of use (thanks to Nim's macros).
How?
Nimja works by compiling its templates to Nim code:
for example this:
is transformed to this:
Since
result &= "mystring
is generated, you can choose any return type, that implements a&=
proc. This makes the process quite flexible. So instead of a string, you could also return aRope
or similar.Nimja can also generate iterator bodies. So instead of string concatenation, it can also yield.
If the web server is able to send data in chunks, you could save some memory, since only small chunks must be stored. In the benchmark, this approach is twice as fast.
Hot Code Reloading
Nimja is a compiled template engine, this means you must recompile your application for every change you do in the templates. This could be quite annoying and time consuming.
To streamline the experience a little bit, Nimja ships with Hot Code Reloading utilities.
This means it can compile templates to a dynamic library and recompile and reload this library on template change.
For details have a look at the
Hot Code Reloading
section of the readme."Fileless"
The templates are compiled to binary and stored in the executable. This means that the templates must not be shipped in cleartext.
In addition Nimjautils has utility functions that helps store small assets in the executable.
eg.:
includeRawStatic
andincludeStaticAsDataurl
this could be used for single executable "electron like" gui libraries.
Looks familiar
I've build Nimja to port jinja2 and twig websites to Nim. This means it tries to be as similar to both template languages as possible, without loosing it's "Nim'ness"
Porting often means to just change a few lines of code, or add another "filter" you've used in one of the other engines.
Give Nimja a try it's fun!
https://github.com/enthus1ast/nimja