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

Compiling constants into template #819

Closed
mu-arch opened this issue May 27, 2023 · 14 comments · Fixed by #853
Closed

Compiling constants into template #819

mu-arch opened this issue May 27, 2023 · 14 comments · Fixed by #853

Comments

@mu-arch
Copy link

mu-arch commented May 27, 2023

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.

@vallentin
Copy link
Collaborator

Can you elaborate on what you mean by constants? Maybe an example.

Because currently, you can render constants, e.g. if you have const NAME: &str = "Bob"; in the same module as your template, then you can use it in the template, by doing {{ self::NAME }}.

@Kijewski
Copy link
Contributor

Kijewski commented May 27, 2023

It's not needed, afaict (as of rustc 1.71, slated to be released on 2023-07-13): rust-lang/rust#109999

@mu-arch
Copy link
Author

mu-arch commented May 27, 2023

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.

@Kijewski
Copy link
Contributor

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?

@mu-arch
Copy link
Author

mu-arch commented May 28, 2023

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.

@djc
Copy link
Owner

djc commented May 30, 2023

Doesn't it Just Work to have {{ crate::CSS_FILENAME }}?

@mu-arch
Copy link
Author

mu-arch commented May 30, 2023

Doesn't it Just Work to have {{ crate::CSS_FILENAME }}?

Yes, I have gone ahead and made this improvement.

@djc
Copy link
Owner

djc commented May 30, 2023

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.

@mu-arch
Copy link
Author

mu-arch commented May 30, 2023

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.

@djc
Copy link
Owner

djc commented May 30, 2023

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.

@Kijewski
Copy link
Contributor

Kijewski commented Jun 2, 2023

Maybe we could speed up printing out such constants by making more function const?

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 const? Or maybe #[inline]?

@GuillaumeGomez
Copy link
Contributor

Doesn't it Just Work to have {{ crate::CSS_FILENAME }}?

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.

@djc
Copy link
Owner

djc commented Aug 9, 2023

Would be great if someone wants to add context to the documentation to explain this!

@GuillaumeGomez
Copy link
Contributor

Sending a PR then.

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

Successfully merging a pull request may close this issue.

5 participants