Skip to content

Commit eb4806a

Browse files
committed
book: document blocks
1 parent b8ac54b commit eb4806a

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

book/src/creating_templates.md

+30
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,36 @@ recognized:
9292
struct HelloTemplate<'a> { ... }
9393
```
9494

95+
* `blocks` (e.g. `blocks = ["title", "content"]`):
96+
automatically generates (a number of) sub-templates that act as if they had a
97+
`block = "..."` attribute. You can access the sub-templates with the method
98+
<code>my_template.as_<em>block_name</em>()</code>, where *`block_name`* is the
99+
name of the block:
100+
```rust,ignore
101+
#[derive(Template)]
102+
#[template(
103+
ext = "txt",
104+
source = "
105+
{% block title %} ... {% endblock %}
106+
{% block content %} ... {% endblock %}
107+
",
108+
blocks = ["title", "content"]
109+
)]
110+
struct News<'a> {
111+
title: &'a str,
112+
message: &'a str,
113+
}
114+
115+
let news = News {
116+
title: "Announcing Rust 1.84.1",
117+
message: "The Rust team has published a new point release of Rust, 1.84.1.",
118+
};
119+
assert_eq!(
120+
news.as_title().render().unwrap(),
121+
"<h1>Announcing Rust 1.84.1</h1>"
122+
);
123+
```
124+
95125
* `escape` (e.g. `escape = "none"`): override the template's extension used for
96126
the purpose of determining the escaper for this template. See the section
97127
on configuring custom escapers for more information.

book/src/features.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,20 @@ The most useful catch-all feature for a quick start might be `"full"`,
7474
which enables all implemented features, i.e.:
7575

7676
```toml
77-
full = ["default", "code-in-doc", "serde_json"]
77+
full = ["default", "blocks", "code-in-doc", "serde_json"]
7878
```
7979

8080
In production or once your project is “maturing” you might want to manually opt-in to any needed
8181
features with a finer granularity instead of depending on `"full"`.
8282

83+
### `"blocks"`
84+
85+
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">
86+
enabled by <code>"full"</code>
87+
</blockquote>
88+
89+
Enables using [the template attribute `blocks`](creating_templates.html#the-template-attribute).
90+
8391
### `"serde_json"`
8492

8593
<blockquote class="right" style="padding:0.5ex 1ex; margin:0 0 1ex 1ex; font-size:80%">

rinja_derive/src/lib.rs

+50
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,56 @@ use rustc_hash::FxBuildHasher;
100100
/// the generated code (`code`) or `all` for both.
101101
/// The requested data will be printed to stdout at compile time.
102102
///
103+
/// ### block
104+
///
105+
/// E.g. `block = "block_name"`
106+
///
107+
/// Renders the block by itself.
108+
/// Expressions outside of the block are not required by the struct, and
109+
/// inheritance is also supported. This can be useful when you need to
110+
/// decompose your template for partial rendering, without needing to
111+
/// extract the partial into a separate template or macro.
112+
///
113+
/// ```rust,ignore
114+
/// #[derive(Template)]
115+
/// #[template(path = "hello.html", block = "hello")]
116+
/// struct HelloTemplate<'a> { ... }
117+
/// ```
118+
///
119+
/// ### blocks
120+
///
121+
/// E.g. `blocks = ["title", "content"]`
122+
///
123+
/// Automatically generates (a number of) sub-templates that act as if they had a
124+
/// `block = "..."` attribute. You can access the sub-templates with the method
125+
/// <code>my_template.as_<em>block_name</em>()</code>, where *`block_name`* is the
126+
/// name of the block:
127+
///
128+
/// ```rust,ignore
129+
/// #[derive(Template)]
130+
/// #[template(
131+
/// ext = "txt",
132+
/// source = "
133+
/// {% block title %} ... {% endblock %}
134+
/// {% block content %} ... {% endblock %}
135+
/// ",
136+
/// blocks = ["title", "content"]
137+
/// )]
138+
/// struct News<'a> {
139+
/// title: &'a str,
140+
/// message: &'a str,
141+
/// }
142+
///
143+
/// let news = News {
144+
/// title: "Announcing Rust 1.84.1",
145+
/// message: "The Rust team has published a new point release of Rust, 1.84.1.",
146+
/// };
147+
/// assert_eq!(
148+
/// news.as_title().render().unwrap(),
149+
/// "<h1>Announcing Rust 1.84.1</h1>"
150+
/// );
151+
/// ```
152+
///
103153
/// ### escape
104154
///
105155
/// E.g. `escape = "none"`

0 commit comments

Comments
 (0)