lib.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #![doc = include_str!("../README.md")]
  2. #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
  3. #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
  4. mod cache;
  5. pub mod config;
  6. mod fs_cache;
  7. #[cfg(feature = "incremental")]
  8. pub mod incremental;
  9. #[cfg(feature = "incremental")]
  10. mod incremental_cfg;
  11. pub mod eval;
  12. pub mod renderer;
  13. pub mod template;
  14. use dioxus_core::{Element, LazyNodes, Scope, VirtualDom};
  15. use std::cell::Cell;
  16. pub use crate::renderer::Renderer;
  17. /// A convenience function to render an `rsx!` call to a string
  18. ///
  19. /// For advanced rendering, create a new `SsrRender`.
  20. pub fn render_lazy(f: LazyNodes<'_, '_>) -> String {
  21. // We need to somehow get the lazy call into the virtualdom even with the lifetime
  22. // Since the lazy lifetime is valid for this function, we can just transmute it to static temporarily
  23. // This is okay since we're returning an owned value
  24. struct RootProps<'a, 'b> {
  25. caller: Cell<Option<LazyNodes<'a, 'b>>>,
  26. }
  27. fn lazy_app<'a>(cx: Scope<'a, RootProps<'static, 'static>>) -> Element<'a> {
  28. let lazy = cx.props.caller.take().unwrap();
  29. let lazy: LazyNodes = unsafe { std::mem::transmute(lazy) };
  30. Some(lazy.call(cx))
  31. }
  32. let props: RootProps = unsafe {
  33. std::mem::transmute(RootProps {
  34. caller: Cell::new(Some(f)),
  35. })
  36. };
  37. let mut dom = VirtualDom::new_with_props(lazy_app, props);
  38. crate::eval::init_eval(dom.base_scope());
  39. _ = dom.rebuild();
  40. Renderer::new().render(&dom)
  41. }
  42. /// A convenience function to render an existing VirtualDom to a string
  43. ///
  44. /// We generally recommend creating a new `Renderer` to take advantage of template caching.
  45. pub fn render(dom: &VirtualDom) -> String {
  46. Renderer::new().render(dom)
  47. }
  48. /// A convenience function to pre-render an existing VirtualDom to a string
  49. ///
  50. /// We generally recommend creating a new `Renderer` to take advantage of template caching.
  51. pub fn pre_render(dom: &VirtualDom) -> String {
  52. let mut renderer = Renderer::new();
  53. renderer.pre_render = true;
  54. renderer.render(dom)
  55. }