Skip to content

Commit

Permalink
derive: document in_doc in doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 20, 2024
1 parent 87dcba6 commit 93eb139
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions book/src/creating_templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ recognized:
name: &'a str,
}
```

* `in_doc` (as `in_doc = true`):
please see the section ["documentation as template code"](#documentation-as-template-code).

* `ext` (as `ext = "txt"`): lets you specify the content type as a file
extension. This is used to infer an escape mode (see below), and some
web framework integrations use it to determine the content type.
Expand All @@ -66,6 +70,7 @@ recognized:
name: &'a str,
}
```

* `print` (as `print = "code"`): enable debugging by printing nothing
(`none`), the parsed syntax tree (`ast`), the generated code (`code`)
or `all` for both. The requested data will be printed to stdout at
Expand All @@ -75,6 +80,7 @@ recognized:
#[template(path = "hello.html", print = "all")]
struct HelloTemplate<'a> { ... }
```

* `block` (as `block = "block_name"`): renders the block by itself.
Expressions outside of the block are not required by the struct, and
inheritance is also supported. This can be useful when you need to
Expand All @@ -85,6 +91,7 @@ recognized:
#[template(path = "hello.html", block = "hello")]
struct HelloTemplate<'a> { ... }
```

* `escape` (as `escape = "none"`): override the template's extension used for
the purpose of determining the escaper for this template. See the section
on configuring custom escapers for more information.
Expand All @@ -93,6 +100,7 @@ recognized:
#[template(path = "hello.html", escape = "none")]
struct HelloTemplate<'a> { ... }
```

* `syntax` (as `syntax = "foo"`): set the syntax name for a parser defined
in the configuration file. The default syntax , "default", is the one
provided by Rinja.
Expand All @@ -101,6 +109,7 @@ recognized:
#[template(path = "hello.html", syntax = "foo")]
struct HelloTemplate<'a> { ... }
```

* `config` (as `config = "config_file_path"`): set the path for the config file
to be used. The path is interpreted as relative to your crate root.
```rust
Expand All @@ -110,6 +119,7 @@ recognized:
```

## Documentation as template code
[#documentation-as-template-code]: #documentation-as-template-code

As an alternative to supplying the code template code in an external file (as `path` argument),
or as a string (as `source` argument), you can also enable the `"code-in-doc"` feature.
Expand Down
23 changes: 23 additions & 0 deletions rinja_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ use syn::parse_quote_spanned;
/// web framework integrations use it to determine the content type.
/// Cannot be used together with `path`.
///
/// ### in_doc
///
/// E.g. `in_doc = true`
///
/// As an alternative to supplying the code template code in an external file (as `path` argument),
/// or as a string (as `source` argument), you can also enable the `"code-in-doc"` feature.
/// With this feature, you can specify the template code directly in the documentation
/// of the template `struct`.
///
/// Instead of `path = "…"` or `source = "…"`, specify `in_doc = true` in the `#[template]`
/// attribute, and in the struct's documentation add a ```` ```rinja ```` code block:
///
/// ```rust,ignore
/// /// ```rinja
/// /// <div>{{ lines|linebreaksbr }}</div>
/// /// ```
/// #[derive(Template)]
/// #[template(ext = "html", in_doc = true)]
/// struct Example<'a> {
/// lines: &'a str,
/// }
/// ```
///
/// ### print
///
/// E.g. `print = "code"`
Expand Down

0 comments on commit 93eb139

Please sign in to comment.