kristapsdz / kcgi

minimal CGI and FastCGI library for C/C++
https://kristaps.bsd.lv/kcgi
ISC License
278 stars 40 forks source link

How to best integrate mustache templating? #34

Closed mbucc closed 5 years ago

mbucc commented 6 years ago

Hi,

I'm a fan of mustache templates and have found a C library (https://gitlab.com/jobol/mustach) that implements mustache. That lib comes bundled with a wrapper for json-c.

As best I can figure, writing a wrapper for kcgi's generated JSON code is a good way to use this library. A mustache wrapper implements the interface (https://gitlab.com/jobol/mustach/blob/master/mustach.h#L53-59):

struct mustach_itf {
    int (*start)(void *closure);
    int (*put)(void *closure, const char *name, int escape, FILE *file);
    int (*enter)(void *closure, const char *name);
    int (*next)(void *closure);
    int (*leave)(void *closure);
};

where

 * @start: Starts the mustach processing of the closure
 *         'start' is optional (can be NULL)
 *
 * @put: Writes the value of 'name' to 'file' with 'escape' or not
 *
 * @enter: Enters the section of 'name' if possible.
 *         Musts return 1 if entered or 0 if not entered.
 *         When 1 is returned, the function 'leave' will always be called.
 *         Conversely 'leave' is never called when enter returns 0 or
 *         a negative value.
 *         When 1 is returned, the function must activate the first
 *         item of the section.
 *
 * @next: Activates the next item of the section if it exists.
 *        Musts return 1 when the next item is activated.
 *        Musts return 0 when there is no item to activate.
 *
 * @leave: Leaves the last entered section

Do you agree this is a sensible approach? And if so, how much work is involved?

kristapsdz commented 6 years ago

It seems pretty straightforward to me. I'd either wrap around kcgijson or use their json-c and replace the bits that use putc and printf and friends and replace them with khttp_write and friends. Keep me posted with what you do!

dertuxmalwieder commented 6 years ago

Joining here because I'm actually interested. I could not find a sane documentation for mustach except reading the code which is basically hit and miss.

I have been planning to write a web application soon™, and while I make quite some progress with Ningle, the one thing that stops me from moving over to C/kcgi is that I could not find a viable HTML template engine for it just yet. As far as I could see, khttp_template() does not have blocks/loops like Jinja2 does (or does it?) and the only native C alternative I could find, SST, seems to be stalled for the time being.

So with this comment I'm basically setting myself on the watchlist for this issue. :)

ghost commented 6 years ago

Does it not make more sense to generate the output in the frontend? So that the JS frontend puts the pieces together and just concentrate on the more important part , putting more effort on a json–ld strategy? From my perspective templating belongs to the tasks of the frontend and that‘s in fact what processing the template is all about. To be more clear the JS should get the json–ld and the mustache template for the given site and put them together. You do not waste computational power to generate the output and if the request is not authorized to see the data which you have to check anyway it will neither see the template or the data. But if only the data or the template is requested you do not need to care about the rest. It is also a more generic approach. @dertuxmalwieder so I do not understand why you think that this is a blocker for using kcgi? @mbucc I am sure there are ways using mustache with JS if you like it that much but i would suggest vanilla JS and HTML if it is about websites @kristapsdz what do you think about this approach?

dertuxmalwieder commented 6 years ago

so I do not understand why you think that this is a blocker for using kcgi?

Because I honestly lack the experience to come up with a solution on my own.

laoshaw commented 6 years ago

I have been a qdecoder user for a while and is interested in kcgi, it seems kcgi is way more complex, hopefully way more powerful after the learning curve. yes I agree at least template engine should be totally independent from the rest and be optional, as in many cases you may use template on the client side instead these days. @opunix that's a debate between server-render vs client-render for html pages, in some cases you may want to render at the server side, for quick loading time, better SEO, or just protecting your source code and its logic when there is a need.

kristapsdz commented 5 years ago

I'm closing this issue because it's not really an issue with kcgi. Thanks!