Comments (14)
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.
from askama.
Doesn't it Just Work to have {{ crate::CSS_FILENAME }}
?
from askama.
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 }}
.
from askama.
It's not needed, afaict (as of rustc 1.71, slated to be released on 2023-07-13): rust-lang/rust#109999
from askama.
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.
from askama.
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?
from askama.
Doesn't it Just Work to have
{{ crate::CSS_FILENAME }}
?
Yes, I have gone ahead and made this improvement.
from askama.
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.
from askama.
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.
from askama.
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.
from askama.
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]
?
from askama.
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.
from askama.
Would be great if someone wants to add context to the documentation to explain this!
from askama.
Sending a PR then.
from askama.
Related Issues (20)
- Template compilation infinite loop / failed cycle detection HOT 4
- Add display_some filter and friends HOT 14
- Float literals are not completely supported HOT 1
- Add support for single quote strings? HOT 7
- Allow output of not prettified JSON data
- Compilation error when using nested template blocks with in a single template file HOT 3
- HELP: about askama_axum::Template HOT 2
- Nested comments causes parser to timeout HOT 2
- Publish all crate versions books HOT 7
- if let chain has a weird behaviour HOT 2
- `include` is not very efficient HOT 3
- 0.13.0 release planning HOT 2
- Add tests for generated code HOT 2
- When using askama_axum, the automatic import of IntoResponse will preferentially import from askama_axum instead of axum::response. HOT 4
- Deny all / more reserved keywords as macro names
- Using block fragments as part of larger template HOT 4
- Unnecessary fields required when using blocks for partial rendering HOT 3
- Add `elif` keyword HOT 2
- Template Rendering of a Result<T + Display, E + Display> Cleanly HOT 3
- Feature/docs request: allow setting escape="none" in askama.toml HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from askama.