#Is there a way to convert rsx! template to a String without dioxus machinery?

1 messages · Page 1 of 1 (latest)

oak salmon
#

I want to use rsx! for some string templating outside of dioxus ecosystem, thus I do not need dioxus app/router/request handler/etc.

  1. Which dependencies do I need to add to Cargo.toml to have just rsx!?
  2. How then to then convert let some_rsx_template = rsx! { "Hello, World!" } to a string?

I know there's a related question at github (https://github.com/DioxusLabs/dioxus/discussions/3645), but the answer is rather short - it just links to https://docs.rs/dioxus-ssr/latest/dioxus_ssr/ docs, and the docs/answer do not take (1) into account - docs use full dioxus install, operate on VDom, and tbh I could not make it work outside of dioxus app context.

Thanks in advance.

GitHub

I would like to print out the html that would be generated by my rsx! element. So far I've found this render_template_to_html function, but it only seems to work with static html, not dynamic c...

stoic olive
#

If you really just want templating then there are plenty of crates that offer similar syntax and just do that. But you'll need to use a different one than the Dioxus one.

#

If you want things like components, signals, and hooks to work then you'll need a Dioxus vdom.

#

Just the vdom isnt all that heavy though

turbid topaz
#

our SSR works by spinning up a virtualdom (which is quite lightweight) and then rendering it

oak salmon
oak salmon
turbid topaz
#

For a minimal cargo.toml

dioxus = { version =  "0.7.1", features = ["ssr", "lib"], default-features = false}

and then the rendering

use dioxus::prelude::*;

fn main() {
    let out = dioxus::ssr::render_element(rsx! {
        div { "hello world!"}
    });

    println!("{}", out);
}
#

you could use dioxus_ssr but it would be a little difficult to make the Element type manually

#

You can keep the Renderer around if performance becomes an issue

oak salmon
#

Will it render if "div" is omitted? Like rsx! { "Hello, World!" }

turbid topaz
#

yep

oak salmon
#

Okay, brb trying it out

#

Okay, that works as intended, many thanks

oak salmon
# turbid topaz yep

Another question, hoping to slightly improve DX:

  1. any way to somehow enable newlines for strings in loops?
  2. any way to still have string interpolation in at least match statement (= promote match to first-class along with for and if)?

So this (working)

use dioxus::prelude::*;

fn main() {
    let hello = "Hello";

    let out = dioxus::ssr::render_element(rsx! {
        for i in (0..=10) {
            for j in (1..=2) {
                match (i, j) {
                    (2, 2) => format!("{i} {j} {hello}, World!\n"),
                    _ => "Empty\n".to_owned(),
                }
            }
        }

    });

    println!("{}", out);
}

may become this (does not currently work)

use dioxus::prelude::*;

fn main() {
    let hello = "Hello";

    let out = dioxus::ssr::render_element(rsx! {
        for i in (0..=10) {
            for j in (1..=2) {
                match (i, j) {
                    (2, 2) => "{i} {j} {hello}, World!",
                    _ => "Empty"
                }
            }
        }

    });

    println!("{}", out);
}

both producing expected

Empty
Empty
Empty
Empty
Empty
2 2 Hello, World!
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
#

I mean I would be happy to use this flavor of rsx as a templating engine sans dioxus.