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

Allow for variable bindings (let statements) inside a template #57

Closed
ghost opened this issue Nov 4, 2016 · 7 comments
Closed

Allow for variable bindings (let statements) inside a template #57

ghost opened this issue Nov 4, 2016 · 7 comments

Comments

@ghost
Copy link

ghost commented Nov 4, 2016

For example:
@{ let a = bit.source.unwrap(); }

@lambda-fairy
Copy link
Owner

Can you give me an example of how such a feature would be used?

I've considered it in the past, but held back because I couldn't find a use case for it.

Note that you can declare variables outside the html! block as well.

@ghost
Copy link
Author

ghost commented Nov 7, 2016

because some situation e.g. @for n in 1..2 {}.
we want to insert normal rust expression to cache something.

@ghost
Copy link
Author

ghost commented Nov 7, 2016

@for n in somethings {
    div {
        @ { // here are any rust normal code.
            let item = n.something.unwrap(); // here item in something is move out, so we cannot unwrap twise.
        }
        h1 { (item.name) }
        a { (item.age) }
    }
}

@lambda-fairy
Copy link
Owner

lambda-fairy commented Nov 7, 2016

Okay, that makes sense -- thanks. I have a feeling we need to go all the way to arbitrary Rust statements to solve your problem though. Maybe just a @let special form would be enough?

I'll need to think about this a bit more.

@lambda-fairy lambda-fairy changed the title Insert custom rust expression in maud macro. Allow for variable bindings (let statements) inside a template Nov 7, 2016
@ghost
Copy link
Author

ghost commented Nov 7, 2016

but maybe this is powerful and smart.

html! {
    @ {
        somefunction(); // I can run any rust code here.
    }
}

@TheNeikos
Copy link
Contributor

TheNeikos commented Nov 7, 2016

I have to say that I didn't need to do this so far, anything that should be run once can be run outside the macro, and the @if let syntax allows for the rest

Note: I'm not trying to argue too hard against it, however it doesn't feel like something that is necessary.

@lambda-fairy
Copy link
Owner

Implemented in 39602a0. The new @let form is pretty much like @if let, except the pattern cannot fail.

@Sackery I don't want to allow arbitrary Rust statements within a template. If your view logic is complicated enough that you need a whole block for it, then that's a sign that it should be in a separate function altogether.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants