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 · 3 comments
Open

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

qrdwtf opened this issue Apr 3, 2024 · 3 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.

@tibbe
Copy link

tibbe commented Nov 21, 2024

Having used htmx + askama for a little while now the duplication and awkwardness of partial vs full pages is one of, if not the biggest pain point. Here's what it looks like for me:

Constraints:

  • I want all my URLs to work both in htmx and not. The latter is important for e.g. bookmarks/link sharing to work.
  • I want a global layout template (think top nav) that needs some data loaded from the DB (e.g. the logged in user, to get the avatar URL).

This ends in sadness. My workflow today looks as follows:

  1. The above ifs in every router plus 2 askama Template structs.
  2. Two .html template files to go with the above structs e.g.

index.html:

{% extends "../layouts/base.html" %}

{% block content %}
{% include "_index.html" %}
{% endblock %}

_index.html:

<h1>Register</h1>
<form hx-post="/register" hx-target="#container" hx-swap="innerHTML" hx-push-url="true">
  <fieldset>
    <div class="form-group">
      <label for="email">Email</label>
      <input class="form-control" name="email" />
    </div>
    <div class="form-group">
      <label for="password">Password</label>
      <input class="form-control" name="password" type="password" />
    </div>
    <input class="btn btn-primary" type="submit" />
  </fieldset>
</form>

These two are needed as the {% extends "../layouts/base.html" %} cannot be made conditional.

This is even before I have tackled including the user in the base template, which will make things even worse because the user only really needs to be loaded in the non-htmx case (as it doesn't change in the htmx case, typically). Also, every route would have to care about the user even though that kind of concern ideally should be dealt with "globally" by some middleware.

Any ideas?

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

3 participants