Code Monkey home page Code Monkey logo

Comments (14)

mu-arch avatar mu-arch commented on August 23, 2024 1

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.

djc avatar djc commented on August 23, 2024 1

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

from askama.

vallentin avatar vallentin commented on August 23, 2024

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.

Kijewski avatar Kijewski commented on August 23, 2024

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

from askama.

mu-arch avatar mu-arch commented on August 23, 2024

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.

Kijewski avatar Kijewski commented on August 23, 2024

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.

mu-arch avatar mu-arch commented on August 23, 2024

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

Yes, I have gone ahead and made this improvement.

from askama.

djc avatar djc commented on August 23, 2024

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.

mu-arch avatar mu-arch commented on August 23, 2024

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.

djc avatar djc commented on August 23, 2024

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.

Kijewski avatar Kijewski commented on August 23, 2024

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.

GuillaumeGomez avatar GuillaumeGomez commented on August 23, 2024

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.

djc avatar djc commented on August 23, 2024

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

from askama.

GuillaumeGomez avatar GuillaumeGomez commented on August 23, 2024

Sending a PR then.

from askama.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.