djc / askama

Type-safe, compiled Jinja-like templates for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ignore templates in HTML comments to sidestep `no field ... exists` and other errors when design iterating

nmay231 opened this issue · comments

commented

As I develop templates, I sometimes define the template with all fields assumed present before I actually define the fields. This means I have to implement the fields now, or comment out the html that uses the fields that are not yet defined. However, I still get compile errors on templates that have errors in html, even if the templated code is in an html comment. For example:

// my_template.rs
#[derive(Template)]
#[template(path = "buggy_comment.html")]
struct MyTemplate {
    attr_exists: bool,
}
<div>
    <p>{{ attr_exists }}</p>
    <!-- <p>{{ no_attr }}</p> -->
</div>

This gives the error no field "no_attr" on type "&MyTemplate".

Of course the simplest solution is deleting the code in the html template, but that messes with undo-history (I can't change other parts of the code without first undoing the deletion and then re-deleting after making the change). Or you could add dummy fields to the struct, but that still slows design iteration.

Technically, this is a breaking change since before it would just render to the comment, but how often is that actually used?

My personal opinion is that all html comments should be stripped from the templates unless explicitly rendered with a .render_with_comments() or maybe .with_options(RenderOptions::comments(true)).render() if you want something more future proof.

Comments are {# #} in jinja.

commented

Well, that's good to know. A good example of the XY problem. Now I just need to get vscode to use jinja comments instead of html ones.

I'll keep this open, however, on the off chance others like this idea, or that it inspires something else useful.

Considering it's already documented in the askama book, I think we can close it.