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

HxBoostedBy macro to avoid redundant if-else for layout #12

Open
qrdwtf opened this issue Apr 3, 2024 · 2 comments
Open

HxBoostedBy macro to avoid redundant if-else for layout #12

qrdwtf opened this issue Apr 3, 2024 · 2 comments

Comments

@qrdwtf
Copy link

qrdwtf commented Apr 3, 2024

Hi!

I'd like to propose a macros feature in addition to HxBoosted extractor.

Currently with axum-htmx we use HxBoosted like this:

async fn get_index(HxBoosted(boosted): HxBoosted) -> impl IntoResponse {
    if boosted {
        // Send a template extending from _partial.html
    } else {
        // Send a template extending from _base.html
    }
}

I propose to add a macro, then it will be:

#[hx_boosted_by(with_layout)]
async fn get_index() -> Html<String> {
    // some impl here

    // <-- page html -->
}

fn with_layout(Html(html): Html<String>) -> Html<String> {
    // Wrap page html into layout if called
}

What this macro does is transforming that function to:

async fn get_index(HxBoosted(boosted): HxBoosted) -> Html<String> {
    // some impl here

    if boosted {
        // <-- page html -->
    } else {
        // <-- with_layout(page html) -->
    }
}

I don't have much experience with Rust but managed to write something close to that macro in this axum-htmx-derive repo. Didn't publish yet, don't think it requires separate package - but needs to be included in axum-htmx (with feature flag).

Currently it has 2 macros: hx_boosted_by and hx_boosted_by_async - if you have any idea on how it can be merged I'm here :)

Let me know what you think.

@robertwayne
Copy link
Owner

I'm okay with this. I think reducing the boilerplate is worth the additional complexity and I'm willing to maintain it long-term. It fits within the spirit of the library.

Are you interested in starting a draft PR for this here? I imagine that most of your downstream work should be generally transferrable, though I haven't had a chance to look through your repo yet.

@qrdwtf
Copy link
Author

qrdwtf commented Apr 4, 2024

That's great! Sure, I'll make a PR.

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