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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! `children_filtered(...)` Tera helper — a page's live children,
//! filtered and ordered.
//!
//! Pairs with [`crate::page_type::PageTypeHandler::children_query`]:
//! the handler hook decides what's in `children` (the typed list the
//! template iterates by default); this template-side function lets a
//! template re-filter, reorder, or truncate that same list at the
//! call site — useful when one template wants two slices of the same
//! tree (e.g. "recent" + "highlighted").
//!
//! Operates purely on the in-memory list installed by the render
//! pipeline; no DB round-trip from the function (same constraint as
//! [`crate::page_url`] — Tera 1.x function args don't see context, so
//! we route the data through a thread-local that's set + cleared
//! around `tera.render`).
//!
//! ## Usage in templates
//!
//! ```text
//! {% set posts = children_filtered(status="published", order_by="-published_at", limit=20) %}
//! {% for p in posts %}
//! <a href="{{ p.url_path }}">{{ p.title }}</a>
//! {% endfor %}
//! ```
//!
//! ## Arguments
//!
//! - `status` — keep only rows where `Page.status` matches the given
//! string. Omit to skip the filter.
//! - `order_by` — sort key. `"field"` ascending, `"-field"`
//! descending. Recognized: `sort_order`, `id`, `title`, `slug`,
//! `depth`, `published_at`, `created_at`, `updated_at`. Unknown
//! field → unsorted (with a debug-level warning). Omit to keep
//! the handler's order.
//! - `limit` — truncate to first N rows after sort. `0` or omitted →
//! no limit.
//! - `parent_id` — optional sanity check; when supplied, the function
//! verifies it matches the rendered page's id and returns an empty
//! list otherwise. The list is always the rendered page's children
//! — handlers that need a different parent should expose a custom
//! ctx var instead.
use std::cell::RefCell;
use std::cmp::Ordering;
use std::sync::Arc;
use tera::{Function, Tera, Value};
use crate::page::Page;
thread_local! {
/// Per-thread `(rendered_page_id, children_list)` pair. Installed
/// for the duration of one `tera.render` call. Holds owned
/// `Page` rows so the function can re-serialize them without
/// keeping a reference to render-local state.
static CURRENT_CHILDREN: RefCell<Option<Arc<ChildrenSnapshot>>>
= const { RefCell::new(None) };
}
struct ChildrenSnapshot {
/// The id of the page being rendered. `parent_id=` kwarg validates
/// against this so a template can't silently filter the wrong
/// list.
rendered_page_id: i64,
children: Vec<Page>,
}
/// RAII guard. While in scope the rendered page's children are
/// readable via `children_filtered(...)`; on drop the thread-local
/// is cleared so a follow-up render on the same tokio worker
/// doesn't inherit stale data.
#[must_use = "the guard clears the thread-local on drop; bind it to a local"]
pub struct ChildrenGuard {
_priv: (),
}
impl Drop for ChildrenGuard {
fn drop(&mut self) {
CURRENT_CHILDREN.with(|cell| {
*cell.borrow_mut() = None;
});
}
}
/// Install the rendered page's children for the duration of the
/// returned guard. `rendered_page_id` is the id `parent_id=` should
/// match — pass the page row's own id.
pub fn install(rendered_page_id: i64, children: Vec<Page>) -> ChildrenGuard {
CURRENT_CHILDREN.with(|cell| {
*cell.borrow_mut() = Some(Arc::new(ChildrenSnapshot {
rendered_page_id,
children,
}));
});
ChildrenGuard { _priv: () }
}
/// Register the `children_filtered` Tera function. Called once at
/// Tera setup — wired from [`crate::urls::register_tera_helpers`].
pub fn register_tera_function(tera: &mut Tera) {
tera.register_function("children_filtered", ChildrenFilteredFn);
}
struct ChildrenFilteredFn;
impl Function for ChildrenFilteredFn {
fn call(&self, args: &std::collections::HashMap<String, Value>) -> tera::Result<Value> {
let snap = CURRENT_CHILDREN.with(|cell| cell.borrow().clone());
let Some(snap) = snap else {
// No render in flight (or the helper is being called from
// a non-rendered Tera invocation, e.g. macro expansion in
// an admin form). Return an empty list rather than erroring
// so the template gracefully renders nothing.
return Ok(Value::Array(Vec::new()));
};
// parent_id sanity check (optional). Mismatch returns empty so
// templates fail-soft instead of silently filtering the wrong
// page's children.
if let Some(pid) = args.get("parent_id").and_then(Value::as_i64) {
if pid != snap.rendered_page_id {
return Ok(Value::Array(Vec::new()));
}
}
let mut out: Vec<Page> = snap.children.clone();
// status filter
if let Some(want) = args.get("status").and_then(Value::as_str) {
out.retain(|p| p.status == want);
}
// order_by — '-field' desc, 'field' asc. Unknown field leaves
// the order untouched but emits a debug log so a typo doesn't
// silently ignore the kwarg.
if let Some(spec) = args.get("order_by").and_then(Value::as_str) {
let (desc, field) = match spec.strip_prefix('-') {
Some(f) => (true, f),
None => (false, spec),
};
if !sort_by_field(&mut out, field, desc) {
tracing::debug!(
target: "rustango_cms::children_filtered",
field,
"children_filtered: unrecognized order_by field; leaving order unchanged",
);
}
}
// limit (0 = unlimited; treat negative as unlimited too)
if let Some(n) = args.get("limit").and_then(Value::as_i64) {
if n > 0 {
out.truncate(n as usize);
}
}
serde_json::to_value(&out).map_err(|e| {
tera::Error::msg(format!(
"children_filtered: serializing filtered pages failed: {e}"
))
})
}
}
/// Return `true` if `field` matched a known sort key; `false`
/// means the caller's input didn't match anything and the order
/// was left as-is. Compares via a stable secondary key (`id`) so
/// ties don't reshuffle between renders.
fn sort_by_field(rows: &mut [Page], field: &str, desc: bool) -> bool {
let cmp: fn(&Page, &Page) -> Ordering = match field {
"sort_order" => |a, b| a.sort_order.cmp(&b.sort_order).then(cmp_id(a, b)),
"id" => cmp_id,
"title" => |a, b| a.title.cmp(&b.title).then(cmp_id(a, b)),
"slug" => |a, b| a.slug.cmp(&b.slug).then(cmp_id(a, b)),
"depth" => |a, b| a.depth.cmp(&b.depth).then(cmp_id(a, b)),
"published_at" => |a, b| a.published_at.cmp(&b.published_at).then(cmp_id(a, b)),
"last_published_at" => |a, b| {
a.last_published_at
.cmp(&b.last_published_at)
.then(cmp_id(a, b))
},
"created_at" => |a, b| {
a.created_at
.get()
.copied()
.cmp(&b.created_at.get().copied())
.then(cmp_id(a, b))
},
"updated_at" => |a, b| {
a.updated_at
.get()
.copied()
.cmp(&b.updated_at.get().copied())
.then(cmp_id(a, b))
},
_ => return false,
};
if desc {
rows.sort_by(|a, b| cmp(b, a));
} else {
rows.sort_by(cmp);
}
true
}
fn cmp_id(a: &Page, b: &Page) -> Ordering {
a.id.get().copied().cmp(&b.id.get().copied())
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{TimeZone, Utc};
use rustango::sql::Auto;
use serde_json::json;
use tera::{Context, Tera};
fn page(id: i64, slug: &str, status: &str, published_at: Option<i64>) -> Page {
Page {
id: Auto::Set(id),
page_type_id: 1,
title: format!("Title {id}"),
slug: slug.to_owned(),
path: format!("{id:04}/"),
url_path: format!("/{slug}"),
preview_path: String::new(),
template_override: String::new(),
depth: 1,
parent_id: Some(99),
locale_variant_of: None,
alias_of: None,
theme_id: None,
sort_order: id as i32,
status: status.to_owned(),
published_at: published_at.map(|t| Utc.timestamp_opt(t, 0).single().unwrap()),
last_published_at: published_at.map(|t| Utc.timestamp_opt(t, 0).single().unwrap()),
go_live_at: None,
expire_at: None,
seo_title: String::new(),
seo_description: String::new(),
robots_index: true,
sitemap_priority: 0.5,
show_in_menus: false,
og_title: String::new(),
og_description: String::new(),
og_image_media_id: None,
twitter_card: String::from("summary_large_image"),
notification_pre_published_sent: false,
created_at: Auto::Set(Utc.timestamp_opt(0, 0).single().unwrap()),
updated_at: Auto::Set(Utc.timestamp_opt(0, 0).single().unwrap()),
}
}
fn fresh_tera() -> Tera {
let mut tera = Tera::default();
register_tera_function(&mut tera);
tera
}
fn render(tera: &Tera, src: &str) -> String {
let mut t = tera.clone();
t.add_raw_template("t.html", src).unwrap();
t.render("t.html", &Context::new()).unwrap()
}
#[test]
fn returns_empty_when_nothing_installed() {
let tera = fresh_tera();
let out = render(&tera, r#"{{ children_filtered() | length }}"#);
assert_eq!(out, "0");
}
#[test]
fn returns_full_list_when_no_filters_applied() {
let _g = install(
99,
vec![page(1, "a", "published", None), page(2, "b", "draft", None)],
);
let tera = fresh_tera();
let out = render(&tera, r#"{{ children_filtered() | length }}"#);
assert_eq!(out, "2");
}
#[test]
fn filters_by_status() {
let _g = install(
99,
vec![
page(1, "a", "published", None),
page(2, "b", "draft", None),
page(3, "c", "published", None),
],
);
let tera = fresh_tera();
let out = render(
&tera,
r#"{% set p = children_filtered(status="published") %}{{ p | length }}|{{ p[0].slug }}"#,
);
assert_eq!(out, "2|a");
}
#[test]
fn orders_by_published_at_desc() {
let _g = install(
99,
vec![
page(1, "a", "published", Some(100)),
page(2, "b", "published", Some(300)),
page(3, "c", "published", Some(200)),
],
);
let tera = fresh_tera();
let out = render(
&tera,
r#"{% for p in children_filtered(order_by="-published_at") %}{{ p.slug }} {% endfor %}"#,
);
assert_eq!(out, "b c a ");
}
#[test]
fn orders_by_title_asc() {
let _g = install(
99,
vec![
page(3, "c", "published", None),
page(1, "a", "published", None),
page(2, "b", "published", None),
],
);
let tera = fresh_tera();
let out = render(
&tera,
r#"{% for p in children_filtered(order_by="title") %}{{ p.slug }} {% endfor %}"#,
);
// titles "Title 1" < "Title 2" < "Title 3" ascending
assert_eq!(out, "a b c ");
}
#[test]
fn limit_truncates_after_sort() {
let _g = install(
99,
vec![
page(1, "a", "published", Some(100)),
page(2, "b", "published", Some(300)),
page(3, "c", "published", Some(200)),
],
);
let tera = fresh_tera();
let out = render(
&tera,
r#"{% for p in children_filtered(order_by="-published_at", limit=2) %}{{ p.slug }} {% endfor %}"#,
);
assert_eq!(out, "b c ");
}
#[test]
fn parent_id_mismatch_returns_empty() {
let _g = install(99, vec![page(1, "a", "published", None)]);
let tera = fresh_tera();
let out = render(&tera, r#"{{ children_filtered(parent_id=42) | length }}"#);
assert_eq!(out, "0");
}
#[test]
fn parent_id_match_passes() {
let _g = install(99, vec![page(1, "a", "published", None)]);
let tera = fresh_tera();
let out = render(&tera, r#"{{ children_filtered(parent_id=99) | length }}"#);
assert_eq!(out, "1");
}
#[test]
fn unknown_order_by_leaves_input_order() {
let _g = install(
99,
vec![
page(3, "c", "published", None),
page(1, "a", "published", None),
],
);
let tera = fresh_tera();
let out = render(
&tera,
r#"{% for p in children_filtered(order_by="bogus_field") %}{{ p.slug }} {% endfor %}"#,
);
// Input order preserved (c, a) — unknown field doesn't sort.
assert_eq!(out, "c a ");
}
#[test]
fn guard_clears_thread_local() {
{
let _g = install(99, vec![page(1, "a", "published", None)]);
let tera = fresh_tera();
assert_eq!(render(&tera, r#"{{ children_filtered() | length }}"#), "1");
}
let tera = fresh_tera();
assert_eq!(render(&tera, r#"{{ children_filtered() | length }}"#), "0");
}
#[test]
fn serializes_full_page_shape() {
// Templates iterate the returned objects, so the shape MUST
// match the canonical Page JSON the public render context
// exposes for `children`. This pins both surfaces in sync.
let _g = install(99, vec![page(7, "post", "published", Some(123))]);
let tera = fresh_tera();
let out = render(
&tera,
r#"{% set p = children_filtered() %}{{ p[0].id }}-{{ p[0].slug }}-{{ p[0].status }}"#,
);
assert_eq!(out, "7-post-published");
// Pin against the same shape used by other ctx vars.
let json = serde_json::to_value(&page(7, "post", "published", Some(123))).unwrap();
assert_eq!(json["id"], json!(7));
assert_eq!(json["status"], json!("published"));
}
}