Skip to content

Commit

Permalink
Add text docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn committed Mar 6, 2019
1 parent 09104b4 commit f682a51
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
40 changes: 39 additions & 1 deletion book/src/html-macro/text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,46 @@
Rather than needing you to wrap your text in `"` quotation marks,
the `html-macro` will work with raw unquoted text.

Here are some examples of how the `html!` macro works with text:
```rust
fn main () {
let interpolated_var = "interpolate text variables.";

let example = html! {
<div>
Text can be typed directly into your HTML.
<div>Or you can also {interpolated_var}</div>
</div>
};
}
```

You should always get the same spacing (or lack there of) between text and/or elements as you would
if you were working in a regular old `.html` file.

When it comes to interpolated variables, we base spacing on the spacing outside of the brackets.

```rust
fn main () {
let text = "hello";

html! { <div>{ hello }</div> }; // <div>hello</div>
html! { <div>{hello}</div> }; // <div>hello</div>

html! { <div> { hello } </div> }; // <div> hello </div>
html! { <div> {hello} </div> }; // <div> hello </div>

html! { <div>{hello} </div> }; // <div>hello </div>
html! { <div> {hello}</div> }; // <div> hello</div>
}
```

## More Examples

Here are a bunch of examples showing you what happens when you try and mix
text nodes / variables / elements.

```rust
// Imported into book from crates/html-macro-test/src/text.rs

{{ #include ../../../../crates/html-macro-test/src/text.rs }}
```
22 changes: 10 additions & 12 deletions crates/html-macro-test/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ use virtual_node::{IterableNodes, VElement, VirtualNode};

#[test]
fn text_root_node() {
HtmlMacroTest {
desc: "Text as root node",
generated: html! { some text },
expected: VirtualNode::text("some text"),
}
.test()
assert_eq!(&html! { some text }.to_string(), "some text");
}

#[test]
fn text_variable_root() {
let text = "hello world";

HtmlMacroTest {
desc: "Text variable root",
generated: html! { { text } },
expected: VirtualNode::text("hello world"),
}
.test()
assert_eq!(&html! { { text } }.to_string(), "hello world");
}

#[test]
fn raw_string_literal() {
assert_eq!(
&html! { <div>{ r#"Hello World"# }</div> }.to_string(),
"<div>Hello World</div>"
);
}

#[test]
Expand Down

0 comments on commit f682a51

Please sign in to comment.