Skip to content

Commit

Permalink
feat(templates): ✨ added template macro to automate template fn cre…
Browse files Browse the repository at this point in the history
…ation

This makes Perseus significantly more ergonomic.

Partially addresses #57.
  • Loading branch information
arctic-hen7 committed Oct 31, 2021
1 parent 4364d99 commit 810ae1b
Show file tree
Hide file tree
Showing 21 changed files with 365 additions and 113 deletions.
17 changes: 6 additions & 11 deletions examples/basic/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use perseus::Template;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[perseus::template(AboutPage)]
#[component(AboutPage<G>)]
pub fn about_page() -> SycamoreTemplate<G> {
template! {
Expand All @@ -9,15 +10,9 @@ pub fn about_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("about")
.template(|_| {
template! {
AboutPage()
}
})
.head(|_| {
template! {
title { "About Page | Perseus Example – Basic" }
}
})
Template::new("about").template(about_page).head(|_| {
template! {
title { "About Page | Perseus Example – Basic" }
}
})
}
9 changes: 2 additions & 7 deletions examples/basic/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct IndexPageProps {
pub greeting: String,
}

#[perseus::template(IndexPage)]
#[component(IndexPage<G>)]
pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {
template! {
Expand All @@ -21,13 +22,7 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {
pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("index")
.build_state_fn(get_build_props)
.template(|props: Option<String>| {
template! {
IndexPage(
serde_json::from_str::<IndexPageProps>(&props.unwrap()).unwrap()
)
}
})
.template(index_page)
.head(|_| {
template! {
title { "Index Page | Perseus Example – Basic" }
Expand Down
7 changes: 2 additions & 5 deletions examples/i18n/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use perseus::{is_server, t, Template};
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[perseus::template(AboutPage)]
#[component(AboutPage<G>)]
pub fn about_page() -> SycamoreTemplate<G> {
template! {
Expand All @@ -18,9 +19,5 @@ pub fn about_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("about").template(|_| {
template! {
AboutPage()
}
})
Template::new("about").template(about_page)
}
7 changes: 2 additions & 5 deletions examples/i18n/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use perseus::{link, t, Template};
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[perseus::template(IndexPage)]
#[component(IndexPage<G>)]
pub fn index_page() -> SycamoreTemplate<G> {
let username = "User";
Expand All @@ -13,9 +14,5 @@ pub fn index_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("index").template(|_| {
template! {
IndexPage()
}
})
Template::new("index").template(index_page)
}
9 changes: 2 additions & 7 deletions examples/i18n/src/templates/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct PostPageProps {
content: String,
}

#[perseus::template(PostPage)]
#[component(PostPage<G>)]
pub fn post_page(props: PostPageProps) -> SycamoreTemplate<G> {
let title = props.title;
Expand All @@ -29,13 +30,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("post")
.build_paths_fn(get_static_paths)
.build_state_fn(get_static_props)
.template(|props| {
template! {
PostPage(
serde_json::from_str::<PostPageProps>(&props.unwrap()).unwrap()
)
}
})
.template(post_page)
}

pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
Expand Down
17 changes: 6 additions & 11 deletions examples/plugins/src/templates/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use perseus::Template;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

// This page will actually be replaced entirely by a plugin!
#[perseus::template(AboutPage)]
#[component(AboutPage<G>)]
pub fn about_page() -> SycamoreTemplate<G> {
template! {
Expand All @@ -10,15 +11,9 @@ pub fn about_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("about")
.template(|_| {
template! {
AboutPage()
}
})
.head(|_| {
template! {
title { "About Page | Perseus Example – Plugins" }
}
})
Template::new("about").template(about_page).head(|_| {
template! {
title { "About Page | Perseus Example – Plugins" }
}
})
}
17 changes: 6 additions & 11 deletions examples/plugins/src/templates/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use perseus::{GenericNode, Template};
use sycamore::prelude::{component, template, Template as SycamoreTemplate};

#[perseus::template(IndexPage)]
#[component(IndexPage<G>)]
pub fn index_page() -> SycamoreTemplate<G> {
template! {
Expand All @@ -10,15 +11,9 @@ pub fn index_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("index")
.template(|_| {
template! {
IndexPage()
}
})
.head(|_| {
template! {
title { "Index Page | Perseus Example – Plugins" }
}
})
Template::new("index").template(index_page).head(|_| {
template! {
title { "Index Page | Perseus Example – Plugins" }
}
})
}
7 changes: 2 additions & 5 deletions examples/showcase/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use perseus::Template;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[perseus::template(AboutPage)]
#[component(AboutPage<G>)]
pub fn about_page() -> SycamoreTemplate<G> {
template! {
Expand All @@ -9,9 +10,5 @@ pub fn about_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("about").template(|_| {
template! {
AboutPage()
}
})
Template::new("about").template(about_page)
}
9 changes: 2 additions & 7 deletions examples/showcase/src/templates/amalgamation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct AmalagamationPageProps {
pub message: String,
}

#[perseus::template(AmalgamationPage)]
#[component(AmalgamationPage<G>)]
pub fn amalgamation_page(props: AmalagamationPageProps) -> SycamoreTemplate<G> {
template! {
Expand All @@ -19,13 +20,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.build_state_fn(get_build_state)
.request_state_fn(get_request_state)
.amalgamate_states_fn(amalgamate_states)
.template(|props| {
template! {
AmalgamationPage(
serde_json::from_str::<AmalagamationPageProps>(&props.unwrap()).unwrap()
)
}
})
.template(amalgamation_page)
}

pub fn amalgamate_states(states: States) -> RenderFnResultWithCause<Option<String>> {
Expand Down
9 changes: 2 additions & 7 deletions examples/showcase/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct IndexPageProps {
pub greeting: String,
}

#[perseus::template(IndexPage)]
#[component(IndexPage<G>)]
pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {
template! {
Expand All @@ -17,13 +18,7 @@ pub fn index_page(props: IndexPageProps) -> SycamoreTemplate<G> {
pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("index")
.build_state_fn(get_static_props)
.template(|props| {
template! {
IndexPage(
serde_json::from_str::<IndexPageProps>(&props.unwrap()).unwrap()
)
}
})
.template(index_page)
}

pub async fn get_static_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
Expand Down
11 changes: 3 additions & 8 deletions examples/showcase/src/templates/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ pub struct IpPageProps {
ip: String,
}

#[perseus::template(IpPage)]
#[component(IpPage<G>)]
pub fn dashboard_page(props: IpPageProps) -> SycamoreTemplate<G> {
pub fn ip_page(props: IpPageProps) -> SycamoreTemplate<G> {
template! {
p {
(
Expand All @@ -21,13 +22,7 @@ pub fn dashboard_page(props: IpPageProps) -> SycamoreTemplate<G> {
pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("ip")
.request_state_fn(get_request_state)
.template(|props| {
template! {
IpPage(
serde_json::from_str::<IpPageProps>(&props.unwrap()).unwrap()
)
}
})
.template(ip_page)
}

pub async fn get_request_state(
Expand Down
7 changes: 2 additions & 5 deletions examples/showcase/src/templates/new_post.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use perseus::Template;
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

#[perseus::template(NewPostPage)]
#[component(NewPostPage<G>)]
pub fn new_post_page() -> SycamoreTemplate<G> {
template! {
Expand All @@ -9,9 +10,5 @@ pub fn new_post_page() -> SycamoreTemplate<G> {
}

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("post/new").template(|_| {
template! {
NewPostPage()
}
})
Template::new("post/new").template(new_post_page)
}
9 changes: 2 additions & 7 deletions examples/showcase/src/templates/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct PostPageProps {
content: String,
}

#[perseus::template(PostPage)]
#[component(PostPage<G>)]
pub fn post_page(props: PostPageProps) -> SycamoreTemplate<G> {
let title = props.title;
Expand All @@ -29,13 +30,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
.build_paths_fn(get_static_paths)
.build_state_fn(get_static_props)
.incremental_generation()
.template(|props| {
template! {
PostPage(
serde_json::from_str::<PostPageProps>(&props.unwrap()).unwrap()
)
}
})
.template(post_page)
}

pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
Expand Down
9 changes: 2 additions & 7 deletions examples/showcase/src/templates/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct TimePageProps {
pub time: String,
}

#[perseus::template(TimePage)]
#[component(TimePage<G>)]
pub fn time_page(props: TimePageProps) -> SycamoreTemplate<G> {
template! {
Expand All @@ -18,13 +19,7 @@ pub fn time_page(props: TimePageProps) -> SycamoreTemplate<G> {

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("timeisr")
.template(|props| {
template! {
TimePage(
serde_json::from_str::<TimePageProps>(&props.unwrap()).unwrap()
)
}
})
.template(time_page)
// This page will revalidate every five seconds (to illustrate revalidation)
.revalidate_after("5s".to_string())
.incremental_generation()
Expand Down
9 changes: 2 additions & 7 deletions examples/showcase/src/templates/time_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct TimePageProps {
pub time: String,
}

#[perseus::template(TimePage)]
#[component(TimePage<G>)]
pub fn time_page(props: TimePageProps) -> SycamoreTemplate<G> {
template! {
Expand All @@ -16,13 +17,7 @@ pub fn time_page(props: TimePageProps) -> SycamoreTemplate<G> {

pub fn get_template<G: GenericNode>() -> Template<G> {
Template::new("time")
.template(|props| {
template! {
TimePage(
serde_json::from_str::<TimePageProps>(&props.unwrap()).unwrap()
)
}
})
.template(time_page)
// This page will revalidate every five seconds (to illustrate revalidation)
// Try changing this to a week, even though the below custom logic says to always revalidate, we'll only do it weekly
.revalidate_after("5s".to_string())
Expand Down
Loading

0 comments on commit 810ae1b

Please sign in to comment.