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

Assignment inside block does not compile as expected #227

Closed
luqmana opened this issue Mar 24, 2019 · 7 comments
Closed

Assignment inside block does not compile as expected #227

luqmana opened this issue Mar 24, 2019 · 7 comments

Comments

@luqmana
Copy link

luqmana commented Mar 24, 2019

Here's the setup:

base.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>{% block title %}{{ title }}{% endblock %}</title>
  </head>
  <body>
    {% block body %}{% endblock %}
  </body>
</html>

test.html:

{% extends "base.html" %}
{% block title %}Test{% endblock %}
{% block body %}
{% let foo = 123 %}
{{ foo }}
{% endblock %}

main.rs:

se askama::Template;

#[derive(Template)]
#[template(path = "test.html", print = "code")]
struct Test;

fn main() {
    let _ = Test;
}

This fails to compile:

> % cargo build
   Compiling askama-test v0.1.0 (/tmp/askama-test)
impl ::askama::Template for Test {
    fn render_into(&self, writer: &mut ::std::fmt::Write) -> ::askama::Result<()> {
        include_bytes ! ( "/tmp/askama-test/templates/test.html" )
;
        include_bytes ! ( "/tmp/askama-test/templates/base.html" )
;
        let foo = 123;
        write!(
            writer,
            "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\" />\n    <title>Test</title>\n  </head>\n  <body>\n    \n\n{expr0}\n\n  </body>\n</html>",
            expr0 = &::askama::MarkupDisplay::new_unsafe(&self.foo, ::askama::Html),
        )?;
        Ok(())
    }
    fn extension() -> Option<&'static str> {
        Some("html")
    }
    fn size_hint() -> usize {
        142
    }
}
impl ::std::fmt::Display for Test {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        ::askama::Template::render_into(self, f).map_err(|_| ::std::fmt::Error {})
    }
}

error[E0609]: no field `foo` on type `&Test`
 --> src/main.rs:3:10
  |
3 | #[derive(Template)]
  |          ^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0609`.
error: Could not compile `askama-test`.

To learn more, run the command again with --verbose.

But oddly enough if you add an empty loop to the template it works:

{% extends "base.html" %}
{% block title %}Test{% endblock %}
{% block body %}
{% let foo = 123 %}
{{ foo }}
{% for x in [1] %}{% endfor %}        <!-- this line -->
{% endblock %}
@luqmana
Copy link
Author

luqmana commented May 12, 2019

Is there anything else you need from me to investigate this?

@djc
Copy link
Owner

djc commented May 12, 2019

Not really -- it's just been really busy. I hope to get to it soon!

@vallentin
Copy link
Collaborator

Similarly, this also happens inside if.

Assuming the template contains title: Option<&'static str>. Then the following code also results in no field `t` on type `&main::TestTemplate.

{% if title.is_some() %}
    {% let t = title.unwrap() %}
    {{ t }}
{% endif %}

In case someone finds this issue, like I did. Then currently, a possible workaround is to declare the name before the if statement, like this:

{% let t %}
{% if title.is_some() %}
    {%- let t = title.unwrap() %}
    {{ t }}
{% endif %}

@djc
Copy link
Owner

djc commented Jan 12, 2020

Thanks for the reports, and sorry for the long wait. This has now been fixed.

@jhoobergs
Copy link

I think I encountered a similar problem @djc The code below returns an error

{% let birthday_s %}
{% match form_data.birthday -%}
  {% when Some with (birthday) -%}
    {% let birthday_s = birthday.format("%Y-%m-%d").to_string() -%}
  {% when None -%}
    {% let birthday_s = "".to_string() -%}
  {% endmatch -%}
<input type="text" id="birthday" required name="birthday" placeholder="birthday" value="{{birthday_s}}"/>

The error is

no field `birthday_s` on type `&Page<'a>`

@djc
Copy link
Owner

djc commented Jan 14, 2021

Is this with a released version? Might want to try it with master, I think we've had some fixes related to this (if it still fails, please file a new issue).

@vallentin
Copy link
Collaborator

@jhoobergs to reassure what @djc said. That particular example currently works and was fixed in PR #411.

It has not been released yet, so as a workaround, you can change your dependency to use the repository in the meantime.

[dependencies]
askama = { git = "https://github.com/djc/askama", rev = "000aff4a18c90a5074afd1af5587ff94e915e7d1" }

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

4 participants