-
-
Notifications
You must be signed in to change notification settings - Fork 219
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
Compiling constants into template #819
Comments
Can you elaborate on what you mean by constants? Maybe an example. Because currently, you can render constants, e.g. if you have |
It's not needed, afaict (as of rustc 1.71, slated to be released on 2023-07-13): rust-lang/rust#109999 |
Just to clarify this is what I mean: Example HTML: <link rel="stylesheet" type="text/css" href="{{ self.css() }}"> Example Impl of self.css(): impl<'a> DirectoryTemplate<'a> {
fn css(&self) -> &'static str {
crate::CSS_FILENAME
}
} The filename is determined at compile time in a build script. It is then written into the template on each request. |
Sorry, I don't see the need to implement anything for this special case. Unless you are micro-benchmarking this rare corner case, you won't find this line to be your bottleneck. And if it is, if you don't have database access or anything, then why aren't you generating a static site? |
I agree it's impact is minimal. It just would be a nice feature, and I would feel less guilty using many more constants generated in build scripts. In my case the reason I am not generating a static site is I am using this in a HTTP file server (https://github.com/mu-arch/skyfolder) that produces index pages of a user's filesystem. It doesn't really make sense to produce cache files on the user's PC for this, especially when the page content could be changing constantly. |
Doesn't it Just Work to have |
Yes, I have gone ahead and made this improvement. |
So I'm not sure what else it is that you're looking for? You can also declare string variables inside templates which, if you use a string literal, should be equivalent to having a constant. |
Well your reply doesn't respond to my original suggestion (which is not how the filename gets written in, but when it gets written in i.e. at compile time versus on each request) unless I am mistaken about what Askama is doing. That being said I can always build the filenames into the template at compile time using a build script if I want so the point is essentially moot. This would just be a convenience thing. Fine to close this if there's no interest in the idea. |
It's still pretty unclear to me what it is that you actually want that Askama doesn't support. Insofar as you think the things Askama does support are not as efficient, that seems unlikely. |
Maybe we could speed up printing out such constants by making more function I actually don't think this this will speed up either the compiler or the runtime a lot, because 99% or the runtime overhead will be database accesses, etc., but I'm happy if y'all could prove me wrong! @mu-arch, could to have a look if some function are a good candidate to become |
I don't think it's explained in https://djc.github.io/askama/template_syntax.html. Would be nice if it was added to the documentation. Maybe an entry "using constants in templates"? I can send a PR for that if you want. |
Would be great if someone wants to add context to the documentation to explain this! |
Sending a PR then. |
Afaik this is not supported, but it would be a great feature to be able to write constants into templates at compile time, so they don't have to be templated on each request. Use case is build scripts that generate constants.
The text was updated successfully, but these errors were encountered: