macro cercis-rsx

rsx macro for cercis

17 releases (4 stable)

1.2.0 Jun 5, 2024
1.1.0 May 27, 2024
0.3.1 May 12, 2024
0.2.3 May 5, 2024
0.1.10 May 3, 2024

#1234 in Procedural macros

Download history 3/week @ 2024-07-20 6/week @ 2024-07-27 1/week @ 2024-08-03 7/week @ 2024-08-24 12/week @ 2024-08-31 1/week @ 2024-09-07 37/week @ 2024-09-21 32/week @ 2024-09-28 17/week @ 2024-10-12 37/week @ 2024-10-19 16/week @ 2024-10-26 13/week @ 2024-11-02

83 downloads per month
Used in 3 crates (via cercis)

MIT license

21KB
522 lines

cercis-preview

Macro for cercis package

cargo add cercis

Used only with the cercis package

Using examples

For more examples, see cercis

Formatting

Format the data into a string as in format!() macro

all data is transferred to the template by reference

use cercis::prelude::*;

fn main() {
  let name = "Boris";
  
  let page = rsx!(h1 { "Hello {name}!" });

  // output: <h1>Hello Boris!</h1>
  println!("{}", page.render())
}

Attributes

Attributes are written before the tag content as tag: value

use cercis::prelude::*;

fn main() {
  let text_id = "paragraph";
  let text = "Lorem ipsum";

  let page = rsx!(div {
    class: "container",

    h1 { "Hello World!" }
    p {
      id: "{text_id}",
    
      "{text}"
    }
  });

  // output: <div class='container'><h1>Hello World!</h1><p id='paragraph'>Lorem ipsum</p></div>
  println!("{}", page.render())
}

If branching

Using the usual Rust if syntax, you can create branches

use cercis::prelude::*;

fn main() {
  let num = 8;

  let page = rsx!(div {
    if num == 9 {
      span { "Num is 9" }
    }
    if num == 8 {
      span { "Num is 8" }
    }
  });

  // output: <div><span>Num is 8</span></div>
  println!("{}", page.render())
}

Loops

Using the usual Rust for in you can create loops

use cercis::prelude::*;

fn main() {
  let names = vec!["Boris", "Polina", "Igor"];

  let page = rsx!(ol {
    for name in names {
      li { "{name}" }
    }
  });

  // output: <ol><li>Boris</li><li>Polina</li><li>Igor</li></ol>
  println!("{}", page.render())
}

If you have any problems create issue

Dependencies

~230–670KB
~16K SLoC