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

Add example for axum #118

Merged
merged 6 commits into from
Oct 8, 2022
Merged

Add example for axum #118

merged 6 commits into from
Oct 8, 2022

Conversation

vbrandl
Copy link
Contributor

@vbrandl vbrandl commented Oct 2, 2022

I added an example on how to use ructe with the axum framework.
The example should behave exactly the same as the actix example

Copy link
Owner

@kaj kaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks good. One very minor nitpick (it says warp rather than axum in the example text).

examples/axum/templates/page.rs.html Outdated Show resolved Hide resolved
Copy link
Owner

@kaj kaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. Now I've found the time to look closer at the code. Looks good, I'll merge it soonish.

@@ -0,0 +1,29 @@
use axum::response::{Html, IntoResponse};

macro_rules! render {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool macro! Simple enough, rather different from what the other examples do, but I like it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually the same macro as used in the actix example. I didn't investigate further but it seemed the actix example did duplicate one branch in the macro. Here is the macro from actix:

macro_rules! render {
    ($template:path) => (Render(|o| $template(o)));
    ($template:path, $($arg:expr),*) => {{
        use actix_ructe::Render;
        Render(|o| $template(o, $($arg),*))
    }};
    ($template:path, $($arg:expr),* ,) => {{
        use actix_ructe::Render;
        Render(|o| $template(o, $($arg),*))
    }};
}

I'm not too deep into Rust macros so I don't know what the trailing , in tha last branch of the macro changes in comparison to the second branch but it works.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'd forgotten all about that macro. Ah well. 😎

The trailing comma is to match invocations where this is a trailing comma after the last argument, as is common if the argument list is long enogh to be split on more than one line, like

    render!(
        the_template,
        one_argument,
        another_argument, // <-- here
    )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. I think I found a less redundant way by making the trailing comma optional. See cf68d54

examples/axum/src/axum_ructe.rs Show resolved Hide resolved
@kaj kaj merged commit 03e53a3 into kaj:master Oct 8, 2022
kaj added a commit that referenced this pull request Oct 8, 2022
kaj added a commit that referenced this pull request Jan 22, 2023
* Removed backwards compatible aliases for template functions.
  In ructe 0.7.2 and earlier, a template file `page.rs.html` resulted
  in a rust function `templates::page(...)`.
  In 0.7.2, that was changed to `templates::page_html(...)` and the
  old name was kept as a deprecated alias.
  However, since the template functions are usually defined within the
  same crate that defines them, the deprecation warning has usually
  not been shown, and this removal may still be a surprise to some
  users (it was even used in examples up to this change).
* Allow more lifetime arguments to templates in template arguments (PR
  #122, fixes #121). Thanks to @wezm!
* Added axum example (PR #118). Thanks to @vbrandl!
* Updated rsass to 0.27.0 and base64 to 0.21.0.
* Updated dependencies in examples: actix-web 4.2.1, axum 0.6.2,
  env_logger 0.10.0,
* Dropped support for rust edition 2015 in crates that directly uses
  ructe.
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

Successfully merging this pull request may close these issues.

None yet

2 participants