dafky2000 / simplectemplate

Simple C Template library to expand placeholders in a template string, simple alternative to mustache.github.io that doesn't require JSON as data input.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

passing structures to render_template_file()

andy5995 opened this issue · comments

Nothing I need, but I wonder if an option to pass a structure would be a good idea at some point.

TBH, I am not even sure if this is possible using c, maybe c11?. I will certainly look into it more as I am sure it would be preferable. If you find any resources, definitely share them! Would like to stay as native with little dependencies as possible!

Passing structures? Yeah... Although they might have to passed as pointers... I haven't done it for a while.

In fact, here's an example where an array of structures is passed:
https://github.com/andy5995/nettail/blob/aaf3268fd4a026a6cbe608993cfc074ea07a0b12/src/nettail-server.c#L135

As for having a variable list of arguments... Well.. printf() can do it, so I assume that writing a function that can take a variable list of args would be rather easy.

No special libraries required.

Sorry, I should have been more clear. It is not possible to enumerate member names of a struct in order to match the template variables to the struct members or vise-versa.

Maybe I misunderstood, you mean like this?

struct template_vars { const char* item_name, const char* item_value };
struct template_vars item_list[] = { 
    "data.key", "replacement",
    "data.key2", "replacement2",
    ....
};
render_template("My {{data.key}} template {{data.key2}}", 2, item_list);

That should be relatively straight forward and cuts down on some variable required. I might just do that anyways as a function override. I'm all for cutting the lines of code down for users :)

Oh, looks like I gave you a good idea without meaning to! Great! :D

But what I meant is a situation in which an end-user's program is using a structure to hold data, as opposed to using only arrays (like in mhwkb).

So render_template_file() (from develop) could accept a structure as an argument, as well as arrays.

Sorry, I should have been more clear. It is not possible to enumerate member names of a struct in order to match the template variables to the struct members or vise-versa.

I didn't fully understand this, but that's all right. Perhaps it's time my part of this discussion ended. ;)

Closing and opening a new issue for passing a structured array as described in #35 (comment)