feat(slhx): render hemplate views into slots
Add render_html plus slot render_view helpers that call hemplate render_into, and migrate a kanban effect path from explicit SafeHtml/html handling to the renderable-view helper. req: view/001 req: html_safety/002
This commit is contained in:
@@ -7,5 +7,6 @@ edition.workspace = true
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
hemplate = { path = "../../hemplate/hemplate" }
|
||||
slhx-core = { path = "../slhx-core" }
|
||||
slhx-derive = { path = "../slhx-derive" }
|
||||
|
||||
@@ -6,11 +6,52 @@
|
||||
pub use slhx_core::*;
|
||||
pub use slhx_derive::{app, component, form, handler, surface};
|
||||
|
||||
pub fn render_html(view: &impl hemplate::Hemplate) -> SafeHtml {
|
||||
let mut html = String::new();
|
||||
view.render_into(&mut html)
|
||||
.expect("hemplate view renders into slhx effect payload");
|
||||
SafeHtml::trusted(html)
|
||||
}
|
||||
|
||||
pub trait RenderSlotExt {
|
||||
fn render_view(self, view: &impl hemplate::Hemplate) -> Effect;
|
||||
}
|
||||
|
||||
impl<T> RenderSlotExt for Slot<T> {
|
||||
fn render_view(self, view: &impl hemplate::Hemplate) -> Effect {
|
||||
self.html(render_html(view))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RenderKeyedSlotExt<K> {
|
||||
fn append_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
||||
fn prepend_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
||||
fn replace_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
||||
}
|
||||
|
||||
impl<K, T> RenderKeyedSlotExt<K> for KeyedSlot<K, T>
|
||||
where
|
||||
K: ToString,
|
||||
{
|
||||
fn append_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
||||
self.append_html(key, render_html(view))
|
||||
}
|
||||
|
||||
fn prepend_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
||||
self.prepend_html(key, render_html(view))
|
||||
}
|
||||
|
||||
fn replace_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
||||
self.replace_html(key, render_html(view))
|
||||
}
|
||||
}
|
||||
|
||||
pub mod prelude {
|
||||
pub use slhx_core::{
|
||||
navigate, push, redirect, replace, Atom, BuildFingerprint, ComponentRef, CssClass,
|
||||
CssClasses, Effect, EventName, Form, FormModel, FormValue, Handle, IntoEffect, KeyedSlot,
|
||||
Slot,
|
||||
};
|
||||
pub use crate::{render_html, RenderKeyedSlotExt, RenderSlotExt};
|
||||
pub use slhx_derive::{app, component, form, handler, surface};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user