Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Proposal] Utilize macros #101

Open
gpluscb opened this issue Apr 22, 2021 · 1 comment
Open

[Feature Proposal] Utilize macros #101

gpluscb opened this issue Apr 22, 2021 · 1 comment

Comments

@gpluscb
Copy link

gpluscb commented Apr 22, 2021

It would be nice if templates could be rendered without using build scripts.
The current approach is not very bad, but it could be more ergonomic with macros.

The pro for using macros is that special build scripts would be unnecessary, and this crate would no longer need "special treatment" to set up. It might also be friendlier for development tools, but I am not entirely sure on this point.

The con is that, as far as I understand, this would require setting up another ructe-codegen crate that hosts the procedural macro(s). This might also require exposing some previously private apis in altered form.

For what an api using macros could look like, something like this comes to mind:

let mut buf = Vec::new();
render_template!("templates/hello_args_two.rs.html", &mut buf, 25, "prime", false);

At least naively implemented, this has the disadvantage that templates will need to be parsed once for every use of that template. Also having so many arguments is just not very pretty or readable.

Alternatively, something like

include_template!("templates/hello_args_two.rs.html");
// ...
fn do_template_stuff() {
    let mut buf = Vec::new();
    args_two_html(&mut buf, 25, "prime", false);
    // ...
}

might be worth considering even though it is imo less intuitive because the macro looks more like a function call than a function definition.

let hello_args = template!("templates/hello_args_two.rs.html");
let mut buf = Vec::new();
hello_args.render(&mut buf, 25, "prime", false);

where template! returns an instance of some struct generated from within the macro might also work.

As you can tell, this idea is not very fleshed out yet, but I believe it has a lot of potential in making the library easier to use, and I would love to hear ideas and opinions on the matter.

@kaj
Copy link
Owner

kaj commented Apr 28, 2021

Yes, some way of working as a macro has been in my long-term vision for ructe pretty much since the start. So It's nice to finally having an issue for it. :-)

I have mainly had something like template_module!("templates") in mind (i.e. a single macro that replaces the include! of generated code in projects using current ructe). Maybe handling one template at a a time (and static files separately) is a good idea, at least for getting started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants