1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
// This module is only a chapter of the documentation. //! This module describes the template syntax used by ructe. //! //! The syntax is inspired by //! [Twirl](https://github.com/playframework/twirl), the Scala-based //! template engine in //! [Play framework](https://www.playframework.com/), //! but of course with rust types expressions instead of scala. //! //! A template consists of three basic parts: //! First a preamble of `use` statements, each prepended by an @ sign. //! Secondly a declaration of the parameters the template takes. //! And third, the template body. //! //! ```text //! @use any::rust::Type; //! //! @(name: &str, items: &[Type]) //! //! <html> //! ... //! </html> //! ``` //! //! The curly brackets, `{` and `}`, is used for blocks (see Loops, //! Conditionals, and Calling other templates below). //! To use them in the template body, they must be escaped as `@{` and //! `@}`. #![allow(non_snake_case)] pub mod a_Value_expressions { //! A value expression can be as simple as `@name` to get the value of //! a parameter, but more complicated expressions, including function //! calls, are also allowed. //! //! # Value expressions //! //! A parameter can be used in an expression preceded by an @ sign. //! //! ```text //! <h1>@name</h1> //! ``` //! //! If a parameter is a struct or a trait object, its fields or methods can //! be used, and if it is a callable, it can be called. //! //! ```text //! <p>The user @user.name has email @user.get_email().</p> //! <p>A function result is @function(with, three, arguments).</p> //! ``` //! //! Standard function and macros can also be used, e.g. for specific //! formatting needs: //! //! ```text //! <p>The value is @format!("{:.1}", float_value).</p> //! ``` //! //! If more complex expressions are needed, they can be put in //! parenthesis. //! //! ```text //! <p>The sum @a+3 is @(a+3).</p> //! ``` //! //! Anything is allowed in parenthesis, as long as parenthesis, //! brackets and string quotes are balanced. //! Note that this also applies to the parenthesis of a function //! call or the brackets of an index, so complex things like the //! following are allowed: //! //! ```text //! <p>Index: @myvec[t.map(|s| s.length()).unwrap_or(0)].</p> //! <p>Argument: @call(a + 3, |t| t.something()).</p> //! ``` } pub mod b_Loops { //! A ructe `@for` loop works just as a rust `for` loop, //! iterating over anything that implements `std::iter::IntoIterator`, //! such as a `Vec` or a slice. //! //! # Loops //! //! Rust-like loops are supported like this: //! //! ```text //! <ul>@for item in items { //! <li>@item</li> //! }</ul> //! ``` //! //! Note that the thing to loop over (items, in the example) is a rust //! expression, while the contents of the block is template code. //! //! If items is a slice of tuples (or really, anything that is //! iterable yielding tuples), it is possible to deconstruct the //! tuples into separate values directly: //! //! ```text //! @for (n, item) in items.iter().enumerate() { //! <p>@n: @item</p> //! } //! ``` //! //! It is also possible to loop over a literal array (which may be //! an array of tuples), as long as you do it by reference: //! //! ```text //! @for &(name, age) in &[("Rasmus", 44), ("Mike", 36)] { //! <p>@name is @age years old.</p> //! } //! ``` } pub mod c_Conditionals { //! Both `@if` statements with boolean expressions and match-like //! guard `@if let` statements are supported. //! //! # Conditionals //! //! Rust-like conditionals are supported in a style similar to the loops: //! //! ```text //! @if items.is_empty() { //! <p>There are no items.</p> //! } //! ``` //! //! Pattern matching let expressions are also supported, as well as an //! optional else part. //! //! ```text //! @if let Some(foo) = foo { //! <p>Foo is @foo.</p> //! } else { //! <p>There is no foo.</p> //! } //! ``` //! //! General rust `match` statements are _not_ supported in ructe //! (at least not yet). } pub mod d_Calling_other_templates { //! The ability to call other templates for from a template makes //! both "tag libraries" and "base templates" possible with the //! same syntax. //! //! # Calling other templates //! //! While rust methods can be called as a simple expression, there is a //! special syntax for calling other templates: //! `@:template_name(template_arguments)`. //! Also, before calling a template, it has to be imported by a `use` //! statement. //! Templates are declared in a `templates` module. //! //! So, given something like this in `header.rs.html`: //! //! ```text //! @(title: &str) //! //! <head> //! <title>@title</title> //! <link rel="stylesheet" href="/my/style.css" type="text/css"> //! </head> //! ``` //! //! It can be used like this: //! //! ```text //! @use templates::header; //! //! @() //! //! <html> //! @:header("Example") //! <body> //! <h1>Example</h1> //! <p>page content ...</p> //! </body> //! </html> //! ``` //! //! It is also possible to send template blocks as parameters to templates. //! A structure similar to the above can be created by having something like //! this in `base_page.rs.html`: //! //! ```text //! @(title: &str, body: Content) //! //! <html> //! <head> //! <title>@title</title> //! <link rel="stylesheet" href="/my/style.css" type="text/css"> //! </head> //! <body> //! <h1>@title</h1> //! @:body() //! </body> //! </html> //! ``` //! //! And use it like this: //! //! ```text //! @use templates::base_page; //! //! @() //! //! @:base_page("Example", { //! <p>page content ...</p> //! }) //! ``` }