pkulchenko / fullmoon

Fast and minimalistic Redbean-based Lua web framework in one file.
MIT License
684 stars 30 forks source link

Custom template engines? #8

Closed rashadg1030 closed 2 years ago

rashadg1030 commented 2 years ago

I was wondering if it would be possible to add custom template engines to Fullmoon?

rashadg1030 commented 2 years ago

I was thinking of using something like Aspect for example: https://unifire-app.github.io/aspect/ It's just because I have some people that would be more comfortable with Jinja-like templates. Is the templating language that Fullmoon uses made specifically for Fullmoon?

pkulchenko commented 2 years ago

I was wondering if it would be possible to add custom template engines to Fullmoon?

@rashadg1030, I don't see any reason why not. The library provides a simple way to use the integrated templates, but every setTemplate call accepts a function as its second parameter, which will be called when the template generation is requested, so it should be possible to build and output results produced by another templating engine.

Is the templating language that Fullmoon uses made specifically for Fullmoon?

Yes. The one provided with fullmoon is really simple, but sufficiently powerful: it accepts any Lua code, but doesn't spend much time parsing it (Aspect is using AST processing to parse its templates) and compiles the result into a Lua function that gets executed to generate the template code and output it directly to the output buffer of Redbean without storing intermediate results. Its functionality is more limited than what Aspect and other libraries provide (for example, there is currently no support for blocks), but the entire templating code is 100-120 lines (not including tests), which makes it more amenable for studying and extending.

There is another templating engine (table based) that can be used to generate any code (for example, non-html-based one) and these two engines can be used independently from each other. Their interfaces are still experimental, so they are not well documented yet (and there some things I'm not certain/happy about), but there is a variety of tests and I'm using them with my (relatively complex) projects, so they've been tested on some real-world scenarios.

I'd be interested to see if you run into any issues integrating with an external engine and we can use this ticket to capture and discuss any issues you may run into.

rashadg1030 commented 2 years ago

@pkulchenko Thank you for your response. After discussion with my team, we may not need to plug in an external template engine but this is subject to change in the future. If we end up needing that functionality I'll try to implement it and get back to you with any questions/issues I have.

I must say this stack is pretty amazing. Redbean is great, and what you've built on top of it makes it even more accessible. This project is getting me into Lua, so thanks for that.

pkulchenko commented 2 years ago

Sounds good; thank you for the update. I'll be adding more template-related documentation, so don't hesitate to let me know if you have any questions or something is not clear in the examples.