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

Generate arbitrary HTML fragments with simple anonymous functions #743

Closed
kellytk opened this issue Nov 15, 2019 · 1 comment · Fixed by #783
Closed

Generate arbitrary HTML fragments with simple anonymous functions #743

kellytk opened this issue Nov 15, 2019 · 1 comment · Fixed by #783
Labels
feature-request A feature request

Comments

@kellytk
Copy link
Contributor

kellytk commented Nov 15, 2019

Description

I'm submitting a ...

  • feature request

I would like to be able to generate HTML fragments with anonymous functions.

// component/mod.rs
fn view(&self) -> Html<Self> {
    html! {
        <div>
            crate::utils::view_button(String::from("ok"))
        </div>
    }
}

// utils/mod.rs
pub fn view_button(label: String) -> Html {
    html! {
        <button
            class=Classes::new().extend("custom-button"),
            type="button">
                { label }
        </button>
    }
}
@jstarry
Copy link
Member

jstarry commented Nov 16, 2019

@kellytk thanks for the feature request. I think maybe we just need better documentation around this.

I think if you want reusable fragments, you can use two different patterns:

  1. Create a new Component
  • In your example, you would create a reusable Button component
  1. Create a generic function
  • In your example, you would write view_button<C>(label: String) -> Html<C>

In your example code, you still would probably want a way to attach a listener to the button. That's why we need generics. Otherwise we can't type check that the listener callback can be handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature request
Projects
None yet
2 participants