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

Question: passing a const, not as a field. #291

Closed
nic-hartley opened this issue Jan 27, 2020 · 2 comments
Closed

Question: passing a const, not as a field. #291

nic-hartley opened this issue Jan 27, 2020 · 2 comments

Comments

@nic-hartley
Copy link

I'm trying to reference a constant in my template, since I reuse it all over the place (including across templates) and it'd be a pain to have to update it everywhere it needs to change. I have a template file with {{ date.format(FMT_STR) }}, a const FMT_STR: &'static str = "...";, and a date: chrono::naive::NaiveDate. As expected, this produces self.date.format(self.FMT_STR). How can I get self.date.format(FMT_STR) instead? i.e. How can I refer to my constant, instead of a field?

@nic-hartley nic-hartley changed the title Question Question: passing a const, not as a field. Jan 27, 2020
@djc
Copy link
Owner

djc commented Jan 27, 2020

As a workaround, try referencing it through a path, for example, from the crate root?

Askama tries not to use its knowledge of the context fields, and instead keeps track of local variables, such that everything non-local is treated as a context field. Maybe this can be improved, although I think there were some benefits to the current state. Alternatively, since we know that all-uppercase names are never fields, we could probably hack our way around this. Would you be able to take a swing at this?

It would be as simple as modifying the function here: https://github.com/djc/askama/blob/master/askama_derive/src/generator.rs#L1137 to stop it from prepending self.. If you can also add a test, that would be nice too.

@nic-hartley
Copy link
Author

I can certainly try. It doesn't seem like a particularly complex change, with the exact function being pointed out. I'm gonna be busy for a few days, though. For now I'll just use the workaround. Thanks!

vallentin added a commit that referenced this issue Jan 11, 2021
vallentin added a commit that referenced this issue Jan 12, 2021
@djc djc closed this as completed in 912bbeb Jan 13, 2021
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