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

Add a render_with_values method? #48

Open
GuillaumeGomez opened this issue Jul 4, 2024 · 6 comments
Open

Add a render_with_values method? #48

GuillaumeGomez opened this issue Jul 4, 2024 · 6 comments

Comments

@GuillaumeGomez
Copy link
Contributor

It could allow to create variables with given values before generating the template. Could be useful if there are variables outside of the type.

@GuillaumeGomez
Copy link
Contributor Author

Not possible, however we can add a new API to the Template proc-macro to allow to add values at compile-time.

@ronnodas
Copy link

ronnodas commented Aug 20, 2024

Do you mean it's not possible because the type of the value passed needs to be available at compile time? Because right now, given a template type T and a value type V you can create a wrapper struct (oversimplified)

#[derive(Template(...))]
struct WithValue {
    template: &T,
    values: &V,
}

except the fields of T are not available without prefixing template.. I was imagining an extension to the Template derive that would do this automatically and be used like:

#[derive(Template(with_value = V, ...))]
struct T {
   ...
}

Making it generic over V seems harder, although one possibility is to essentially replace the type V with impl ValueForT for a new trait.

@GuillaumeGomez
Copy link
Contributor Author

The problem is that we only have access to the fields of the current struct, no other information. So for this feature to work, if it's at runtime, it means that all unknown fields like {{ x }} will need to be generated differently. Let's say we take a hashmap into the render_with_values() method. Then for each unknown variable, we'd replace them with getting a value from the hashmap. I'm not sure if it's a good idea in this case.

The other approach I suggested is to instead list values in the template proc-macro directly. Like that, we can still generate the best possible code. However, since it's in a proc-macro, the kind of values which can be used will be limited to booleans, integers, strings, tuples and maybe slices. So I'm not sure if it's a good idea either. It's a tricky feature.

@GuillaumeGomez
Copy link
Contributor Author

I have a use case for such a method in docs.rs where we have a csp_nonce variable which is only generated when rendering the item. So it's a "dead" field until then. Now I'm thinking that we could maybe introduce a "run-time" kind of variable/struct. Like rinja_vars.get::<TYPE>("VAR_NAME")? What do you think @Kijewski ?

@Kijewski
Copy link
Collaborator

Kijewski commented Sep 2, 2024

So, it's like a Cell<Option<_>> that is filled when first used?

Sorry, I'm on vacation right now, and I won't give any programming question a deeper thought until next Sunday. :) If you want to, maybe you could tell me / link the lines of code that could use that feature and I'll have a look when I'm back home.

@GuillaumeGomez
Copy link
Contributor Author

Nah, please don't answer until you're back from holiday. We can discuss when you're back, there is no hurry (I'm in holiday too, yeay).

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

3 participants