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

Can't start a template with a { brace #24

Closed
anowell opened this issue Aug 2, 2017 · 4 comments
Closed

Can't start a template with a { brace #24

anowell opened this issue Aug 2, 2017 · 4 comments

Comments

@anowell
Copy link
Contributor

anowell commented Aug 2, 2017

Simply starting a template file with a single { character (e.g a JSON file) causes the AST and code to be empty:

[]                                                                                                                                                                             
impl< 'a > ::askama::Template for ServiceProxyConfig< 'a > {                                                                                                                   
    fn render_to(&self, writer: &mut ::std::fmt::Write) {                                                                                                                      
    }                                                                                                                                                                          
}    

Prepend an extra space or newline and the template renders fine.

@djc
Copy link
Owner

djc commented Aug 2, 2017

Thanks for the report! I fixed it in 82d85bb and released version 0.3.4 to crates.io, I also added a test case to make sure it doesn't regress.

Let me know if you have any other feedback about the project, please!

@anowell
Copy link
Contributor Author

anowell commented Aug 2, 2017

Thanks!

Re: feedback - Overall, I'm a huge fan. I'm using it for a port of an internal tool that relies on a handful of Jinja2 templates, and in general, I like it much more than Jinja2 itself. I've had a small handful of cases where askama renders invalid templates as empty or missing content (like this issue) - so I'd like to see basically ANY template error break compilation. I've also opted to flatten my template directory for now because I was tripping over the module path when using template inheritance for templates not in the root of the templates directory (might revisit that a bit later). Anyhow, I'll be ramping my usage of askama from my current 4 templates to about 20 over the next couple days, so I might be poking around your issue tracker a bit more ;-)

@djc
Copy link
Owner

djc commented Aug 3, 2017

Great feedback!

  • I'd definitely like Askama to throw an error for invalid templates, rather than rendering empty templates or missing content. I hope the change I did for this issue will help, as it is improves one of the more fragile parts of the parser, but please file bugs also if it doesn't raise an error for templates that you think should be invalid.

  • Can you explain more about the template inheritance issues?

  • Are there any Jinja features that you missed/had to work around?

  • I was a bit surprised about the JSON template. For something like this, I guess I would use serde-json somehow. I was thinking it could make sense to add a feature-gated dependency on serde-json that adds a json filter, so you can just call |json in your templates. Would that be useful for your use cases?

@anowell
Copy link
Contributor Author

anowell commented Aug 3, 2017

template inheritance issues?

I'll recreate the repro for it in the next day or two and create an issue to discuss it in more detail.

any Jinja features that you missed/had to work around

So far nothing has jumped out that I really miss (still early though). I can see that I'll need to work around inclusion of partials. In particular, I have a jinja2 template with this:

        {% include 'partials/'~provider~'-logger-xml.j2' %}

but I don't think that makes as much sense in Askama for 2 reasons:

  1. it's picking the partial to render at runtime via string concatenation
  2. Conditional partials in Askama would require the template struct to have all the fields of all partials which decreases the value of compile-time templates telling you that a particular template needs a particular field. In python the approach was throw everything in a dict and let jinja figure out what to render. With Askama, I'd rather each template struct provide exactly the minimum set of fields needed for a template. So I'm pretty convinced template inheritance is the better answer here anyway. That said, I read that Jinja2 doesn't allow inheritance to repeat the same inheritance block multiple times, and I do need to render the same block multiple times. I haven't gotten to the point of trying that in Askama yet - but my fingers are crossed that Askama didn't inherit that limitation. ;-)

json filter, so you can just call |json in your templates

Sounds neat for some scenarios, but I don't think that would benefit me currently. It's an infra/deployment tool that generates dozens of config files ranging from Play Framework config, to log4j XML configs, to kubernetes YAML files, etc... One of our services depends on a JSON file, and I basically just needed to inject a few values into specific locations within the JSON template.

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