Skip to content

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

@thekujin

Description

@thekujin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions