|
12 | 12 | //! Templates (and static assets) are included in the compiled |
13 | 13 | //! program, which can be a single binary. |
14 | 14 | //! |
| 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 | +//! |
15 | 69 | //! The template syntax, which is inspired by [Twirl], the Scala-based |
16 | 70 | //! template engine in [Play framework], is documented in |
17 | 71 | //! the [Template_syntax] module. |
|
48 | 102 | //! [examples in the repository]: https://github.com/kaj/ructe/tree/master/examples |
49 | 103 | //! [using ructe with warp and diesel]: https://github.com/kaj/warp-diesel-ructe-sample |
50 | 104 | //! |
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 | | -//! |
84 | 105 | //! # Optional features |
85 | 106 | //! |
86 | 107 | //! Ructe has some options that can be enabled from `Cargo.toml`. |
|
95 | 116 | //! * `tide013`, `tide014`, `tide015`, `tide016` -- Support for the |
96 | 117 | //! [tide] framework version 0.13.x through 0.16.x. Implies the |
97 | 118 | //! `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). |
99 | 120 | //! (these versions of tide is compatible enough that the features |
100 | 121 | //! are actually just aliases for the first one, but a future tide |
101 | 122 | //! version may require a modified feature.) |
|
0 commit comments