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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! # Tidos
//!
//! Tidos is a Rust **server-side rendering (SSR)** component framework.
//! Write type-safe HTML components directly in Rust using the [`view!`] and
//! [`page!`] macros — with full support for loops, conditionals, and pattern
//! matching inside your templates.
//!
//! ## Getting started
//!
//! ```toml
//! [dependencies]
//! tidos = "0.7.2"
//!
//! # With Rocket integration:
//! # tidos = { version = "0.7.2", features = ["rocket"] }
//!
//! # With Rocket + internationalization support:
//! # tidos = { version = "0.7.2", features = ["rocket", "i18n"] }
//! ```
//!
//! ## Core concepts
//!
//! | Item | Description |
//! |---|---|
//! | [`view!`] | Renders a fragment of HTML. Returns a `String`. |
//! | [`page!`] | Wraps a full page. Returns a [`Page`] ready to return from a route. |
//! | [`Component`] | Trait for reusable components; implement [`to_render`](Component::to_render). |
//! | [`Page`] | Collects rendered HTML and `<head>` elements for a full page response. |
//! | [`scoped_css!`] | Injects a scoped `<style>` into `<head>` and returns the generated class name. |
//! | [`head!`] | Injects arbitrary HTML into the page `<head>`. |
//! | [`i18n::i18n!`] | *(feature: `i18n`)* Looks up a Fluent translation key. |
//! | [`i18n::enable_i18n!`] | *(feature: `i18n`)* Initialises the translation system in `main.rs`. |
//!
//! ## Defining a component
//!
//! Implement [`Component`] on any struct and use [`view!`] inside
//! [`to_render`](Component::to_render):
//!
//! ```rust,no_run
//! use tidos::{view, Component, Page};
//!
//! pub struct Card {
//! pub title: String,
//! pub body: String,
//! }
//!
//! impl Component for Card {
//! fn to_render(&self, page: &mut Page) -> String {
//! view! {
//! <div class="card">
//! <h2>{&self.title}</h2>
//! <p>{&self.body}</p>
//! </div>
//! }
//! }
//! }
//! ```
//!
//! ## Building a page
//!
//! Use [`page!`] in a route handler to produce a full [`Page`] response.
//! Embed components with JSX-like self-closing tags:
//!
//! ```rust,no_run
//! use tidos::{page, Component, Page};
//!
//! #[get("/")]
//! pub fn index() -> Page {
//! page! {
//! <main>
//! <Card title={ String::from("News") } body={ String::from("Hello world!") } />
//! </main>
//! }
//! }
//! ```
//!
//! ## Template syntax
//!
//! ### Interpolating Rust expressions
//!
//! Wrap any Rust expression in `{ }` to interpolate it:
//!
//! ```rust,no_run
//! use tidos::view;
//!
//! let name = "Alice";
//! let count = 42_usize;
//!
//! view! {
//! <p>Hello {name}, you have {count.to_string()} messages.</p>
//! }
//! ```
//!
//! ### `{#for}` — loops
//!
//! ```rust,no_run
//! use tidos::view;
//!
//! let fruits = vec!["apple", "banana", "cherry"];
//!
//! view! {
//! <ul>
//! {#for fruit in fruits}
//! <li>{fruit}</li>
//! {/for}
//! </ul>
//! }
//! ```
//!
//! ### `{#if}` — conditionals
//!
//! ```rust,no_run
//! use tidos::view;
//!
//! let age = 20_u32;
//! let is_american = false;
//!
//! view! {
//! {#if age >= 18 && !is_american}
//! <p>Allowed to drink.</p>
//! {:else if age >= 21 && is_american}
//! <p>Allowed to drink (US rules).</p>
//! {:else}
//! <p>Not allowed to drink.</p>
//! {/if}
//! }
//! ```
//!
//! ### `{#match}` — pattern matching
//!
//! ```rust,no_run
//! use tidos::view;
//!
//! enum Status { Active, Banned, Guest }
//! let status = Status::Active;
//!
//! view! {
//! {#match status}
//! {:case Status::Active}
//! <span class="green">Active</span>
//! {:case Status::Banned}
//! <span class="red">Banned</span>
//! {:case _}
//! <span class="gray">Guest</span>
//! {/match}
//! }
//! ```
//!
//! ## Scoped CSS
//!
//! [`scoped_css!`] reads a CSS file at compile time, generates a unique class
//! name, and injects a `<style>` block into the page `<head>`. The macro
//! returns the class name as a `&'static str`; apply it to the component's
//! root element. [`Page::add_elements_to_head`] deduplicates by UUID, so
//! calling `scoped_css!` inside a loop is safe.
//!
//! ```rust,no_run
//! use tidos::{scoped_css, view, Component, Page};
//!
//! pub struct Card {
//! pub title: String,
//! }
//!
//! impl Component for Card {
//! fn to_render(&self, page: &mut Page) -> String {
//! view! {
//! <div class={scoped_css!("./card.css")}>
//! <h2>{&self.title}</h2>
//! </div>
//! }
//! }
//! }
//! ```
//!
//! Use CSS nesting so all styles live in one file:
//!
//! ```css
//! & {
//! background: #16213e;
//! border-radius: 8px;
//! }
//!
//! & h2 {
//! color: #a8dadc;
//! }
//! ```
//!
//! ## Injecting `<head>` elements
//!
//! Use [`head!`] inside `to_render` to add arbitrary HTML to the page
//! `<head>` (e.g. a `<title>` or a `<link rel="stylesheet">`):
//!
//! ```rust,no_run
//! use tidos::{head, Component, Page};
//!
//! pub struct Title {
//! pub title: String,
//! }
//!
//! impl Component for Title {
//! fn to_render(&self, page: &mut Page) -> String {
//! head! {
//! <title>{&self.title}</title>
//! }
//! String::new()
//! }
//! }
//! ```
//!
//! ## Internationalization
//!
//! See the [`mod@i18n`] module for full details. Enable the feature flag and call
//! [`i18n::enable_i18n!`] once in `main.rs`:
//!
//! ```toml
//! tidos = { version = "0.7.2", features = ["rocket", "i18n"] }
//! ```
//!
//! ```rust,no_run
//! use tidos::i18n::{enable_i18n, i18n, Lang};
//! use tidos::{view, page, Component, Page};
//!
//! enable_i18n!();
//!
//! pub struct Greeting;
//!
//! impl Component for Greeting {
//! fn to_render(&self, page: &mut Page) -> String {
//! view! {
//! <h1>{i18n!("greeting")}</h1>
//! }
//! }
//! }
//!
//! #[get("/<lang>")]
//! pub fn index(lang: Lang) -> Page {
//! page! { <Greeting /> }
//! }
//! ```
pub use Component;
pub use sanitize;
pub use Page;
pub use *;
/// Internationalization support for Tidos, backed by [Fluent](https://projectfluent.org/).
///
/// Enable with `features = ["i18n"]` in `Cargo.toml`.
///
/// # Setup
///
/// **1.** Call [`enable_i18n!`] once at the top level of `main.rs`.
///
/// **2.** Create a `Tidos.toml` in the project root:
///
/// ```toml
/// [default]
/// resource_location = "translations"
/// default_locale = "en-US"
/// resources = ["common.ftl"]
/// ```
///
/// **3.** Place `.ftl` files under `resource_location`, grouped by locale:
///
/// ```text
/// translations/
/// ├── en-US/
/// │ └── common.ftl
/// └── nl-NL/
/// └── common.ftl
/// ```
///
/// **4.** Add `lang: Lang` to route handlers (locale is the first path
/// segment, e.g. `/en-US`), then call [`i18n!`] inside any component.
///
/// # Example
///
/// ```rust,no_run
/// use tidos::i18n::{enable_i18n, i18n, Lang};
/// use tidos::{view, page, Component, Page};
///
/// enable_i18n!();
///
/// pub struct Greeting;
///
/// impl Component for Greeting {
/// fn to_render(&self, page: &mut Page) -> String {
/// view! {
/// <h1>{i18n!("greeting")}</h1>
/// <p>{i18n!("shared-photos", ("userName", "Anne"), ("photoCount", 3))}</p>
/// }
/// }
/// }
///
/// #[get("/<lang>")]
/// pub fn index(lang: Lang) -> Page {
/// page! { <Greeting /> }
/// }
/// ```