main.rs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #![allow(unused)]
  2. //! Example: Url query segments usage
  3. //! ------------------------------------
  4. //!
  5. //! This example shows how to access and use multiple query segments present in an url on the web.
  6. //!
  7. //! Run `dx serve` and navigate to `http://localhost:8080/blog?name=John&surname=Doe`
  8. use std::fmt::Display;
  9. use dioxus::prelude::*;
  10. use dioxus_router::prelude::*;
  11. // ANCHOR: route
  12. #[derive(Routable, Clone)]
  13. #[rustfmt::skip]
  14. enum Route {
  15. // segments that start with ?:.. are query segments that capture the entire query
  16. #[route("/blog?:..query_params")]
  17. BlogPost {
  18. // You must include query segments in child variants
  19. query_params: ManualBlogQuerySegments,
  20. },
  21. // segments that follow the ?:field&:other_field syntax are query segments that follow the standard url query syntax
  22. #[route("/autoblog?:name&:surname")]
  23. AutomaticBlogPost {
  24. name: String,
  25. surname: String,
  26. },
  27. }
  28. #[derive(Debug, Clone, PartialEq)]
  29. struct ManualBlogQuerySegments {
  30. name: String,
  31. surname: String,
  32. }
  33. /// The display impl needs to display the query in a way that can be parsed:
  34. impl Display for ManualBlogQuerySegments {
  35. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  36. write!(f, "name={}&surname={}", self.name, self.surname)
  37. }
  38. }
  39. /// The query segment is anything that implements <https://docs.rs/dioxus-router/latest/dioxus_router/routable/trait.FromQuery.html>. You can implement that trait for a struct if you want to parse multiple query parameters.
  40. impl FromQuery for ManualBlogQuerySegments {
  41. fn from_query(query: &str) -> Self {
  42. let mut name = None;
  43. let mut surname = None;
  44. let pairs = form_urlencoded::parse(query.as_bytes());
  45. pairs.for_each(|(key, value)| {
  46. if key == "name" {
  47. name = Some(value.clone().into());
  48. }
  49. if key == "surname" {
  50. surname = Some(value.clone().into());
  51. }
  52. });
  53. Self {
  54. name: name.unwrap(),
  55. surname: surname.unwrap(),
  56. }
  57. }
  58. }
  59. #[component]
  60. fn BlogPost(cx: Scope, query_params: ManualBlogQuerySegments) -> Element {
  61. render! {
  62. div{"This is your blogpost with a query segment:"}
  63. div{format!("{:?}", query_params)}
  64. }
  65. }
  66. #[component]
  67. fn AutomaticBlogPost(cx: Scope, name: String, surname: String) -> Element {
  68. render! {
  69. div{"This is your blogpost with a query segment:"}
  70. div{format!("name={}&surname={}", name, surname)}
  71. }
  72. }
  73. #[component]
  74. fn App(cx: Scope) -> Element {
  75. render! { Router::<Route>{} }
  76. }
  77. fn main() {
  78. dioxus_web::launch(App);
  79. }