Skip to content

Commit 87967c0

Browse files
committed
Add a getting started
Try to improve the documentation based on a suggestion by @j-n-f in #142.
1 parent 5cd211d commit 87967c0

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ project adheres to
1111
## Unreleased
1212

1313
* Fixed clippy lints in generated code (PR #143). Thanks @vbrandl!
14+
* Added a clearer "Getting started" section in the docs (PR #145).
15+
Thanks @j-n-f for the suggestion.
1416
* Fixed some clippy lints in ructe itself.
1517
* Updated `itertools` depenendency to 0.14.0.
1618
* Updated optional `rsass` dependency to 0.29.0.

src/lib.rs

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,60 @@
1212
//! Templates (and static assets) are included in the compiled
1313
//! program, which can be a single binary.
1414
//!
15+
//! # Getting started
16+
//!
17+
//! The aim of ructe is to have your templates and static files accessible to
18+
//! your rust code as a module.
19+
//! Since this module will be generated code it will live outside of
20+
//! your `src` directory, so instead of just `use templates`, you will
21+
//! need to include it like this:
22+
//! ```rust,ignore
23+
//! include!(concat!(env!("OUT_DIR"), "/templates.rs"));
24+
//! ```
25+
//!
26+
//! For this to work, you need to tell cargo to build the code.
27+
//! First, specify a build script and ructe as a build dependency in
28+
//! `Cargo.toml`:
29+
//!
30+
//! ```toml
31+
//! build = "src/build.rs"
32+
//!
33+
//! [build-dependencies]
34+
//! ructe = "0.6.0"
35+
//! ```
36+
//!
37+
//! Then, in `build.rs`, compile all templates found in the templates
38+
//! directory and put the output where cargo tells it to:
39+
//!
40+
//! ```rust,no_run
41+
//! use ructe::{Result, Ructe};
42+
//!
43+
//! fn main() -> Result<()> {
44+
//! Ructe::from_env()?.compile_templates("templates")
45+
//! }
46+
//! ```
47+
//!
48+
//! See the docs of the struct [`Ructe`] for details about e.g. adding
49+
//! static files.
50+
//!
51+
//! Now, after putting templates (see below for template syntax) in
52+
//! the `templates` directory, you should be able to render them:
53+
//!
54+
//! ```
55+
//! # // mock
56+
//! # mod templates {
57+
//! # use std::io::{Write, Result};
58+
//! # pub fn hello_html(buf: &mut impl Write, arg: &str) -> Result<()> {
59+
//! # writeln!(buf, "<h1>Hello {arg}!</h1>")
60+
//! # }
61+
//! # }
62+
//! let mut buf = Vec::new();
63+
//! templates::hello_html(&mut buf, "World").unwrap();
64+
//! assert_eq!(buf, b"<h1>Hello World!</h1>\n");
65+
//! ```
66+
//!
67+
//! # Template syntax
68+
//!
1569
//! The template syntax, which is inspired by [Twirl], the Scala-based
1670
//! template engine in [Play framework], is documented in
1771
//! the [Template_syntax] module.
@@ -48,39 +102,6 @@
48102
//! [examples in the repository]: https://github.com/kaj/ructe/tree/master/examples
49103
//! [using ructe with warp and diesel]: https://github.com/kaj/warp-diesel-ructe-sample
50104
//!
51-
//! To be able to use this template in your rust code, you need a
52-
//! `build.rs` that transpiles the template to rust code.
53-
//! A minimal such build script looks like the following.
54-
//! See the [`Ructe`] struct documentation for details.
55-
//!
56-
//! ```rust,no_run
57-
//! use ructe::{Result, Ructe};
58-
//!
59-
//! fn main() -> Result<()> {
60-
//! Ructe::from_env()?.compile_templates("templates")
61-
//! }
62-
//! ```
63-
//!
64-
//! When calling a template, the arguments declared in the template will be
65-
//! prepended by a `Write` argument to write the output to.
66-
//! It can be a `Vec<u8>` as a buffer or for testing, or an actual output
67-
//! destination.
68-
//! The return value of a template is `std::io::Result<()>`, which should be
69-
//! `Ok(())` unless writing to the destination fails.
70-
//!
71-
//! ```
72-
//! # // mock
73-
//! # mod templates {
74-
//! # use std::io::{Write, Result};
75-
//! # pub fn hello_html(buf: &mut impl Write, arg: &str) -> Result<()> {
76-
//! # writeln!(buf, "<h1>Hello {arg}!</h1>")
77-
//! # }
78-
//! # }
79-
//! let mut buf = Vec::new();
80-
//! templates::hello_html(&mut buf, "World").unwrap();
81-
//! assert_eq!(buf, b"<h1>Hello World!</h1>\n");
82-
//! ```
83-
//!
84105
//! # Optional features
85106
//!
86107
//! Ructe has some options that can be enabled from `Cargo.toml`.
@@ -95,7 +116,7 @@
95116
//! * `tide013`, `tide014`, `tide015`, `tide016` -- Support for the
96117
//! [tide] framework version 0.13.x through 0.16.x. Implies the
97118
//! `http-types` feature (but does not require a direct http-types
98-
//! requirement, as that is reexported by tide).
119+
//! dependency, as that is reexported by tide).
99120
//! (these versions of tide is compatible enough that the features
100121
//! are actually just aliases for the first one, but a future tide
101122
//! version may require a modified feature.)

0 commit comments

Comments
 (0)