enthus1ast / nimja

typed and compiled template engine inspired by jinja2, twig and onionhammer/nim-templates for Nim.
MIT License
185 stars 11 forks source link

javascript target/karax nimja2karax? #13

Closed enthus1ast closed 2 years ago

enthus1ast commented 3 years ago

In theory nimja templates could also be used in the browser, for this we should leverage on karax, since it is the most advanced spa framework for nim.

nimja2karax?

ajusa commented 2 years ago

For better performance when using Nimja's JS mode, it's be nice if it could use the native string type the way Karax does.

kstring is Karax's alias for cstring (which stands for "compatible string"; for the JS target that is an immutable JavaScript string) which is preferred for efficiency on the JS target. However, on the native targets kstring is mapped to string for efficiency. The DSL for HTML construction is also avaible for the native targets (!) and the kstring abstraction helps to deal with these conflicting requirements.

I believe Nim's strings are implemented as arrays in JS mode, whereas cstring is a native JS string so operations are much faster.

enthus1ast commented 2 years ago

You can freefly define the result type, as long as there is add / &= proc for the type.

proc foo(): myStringType =

Do you use nimja in a js context? Do you have code online? Would be nice to have a look.

ajusa commented 2 years ago

Ah, I wasn't aware that it would simply work if those procs are defined for the type. I don't have code online yet.

I've used Karax before to do server side rendering in Cloudflare workers. Cloudflare requires you to write JS code, so I figured it'd be good for me to check that I'd be able to use Nimja in that context as well. I'm working on some other projects that'd make it nice to have a fast templating library in the JS backend. I'll let you know once any of it is on Github!

enthus1ast commented 2 years ago

just tried nimja for node:

import nimja
import std/jsconsole

proc foo(): cstring =
  compileTemplateStr "{%if true%}TRUE!!{%endif%}"

console.log foo()

compile with:

nim js -d:nodejs -d:release myfile.nim