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
use std::io::Write;
use liquid_core::error::ResultLiquidExt;
use liquid_core::model::Value;
use liquid_core::Language;
use liquid_core::Renderable;
use liquid_core::Result;
use liquid_core::Runtime;
use liquid_core::Template;
use liquid_core::{BlockReflection, ParseBlock, TagBlock, TagTokenIter};
#[derive(Copy, Clone, Debug, Default)]
pub struct CaptureBlock;
impl CaptureBlock {
pub fn new() -> Self {
Self::default()
}
}
impl BlockReflection for CaptureBlock {
fn start_tag(&self) -> &str {
"capture"
}
fn end_tag(&self) -> &str {
"endcapture"
}
fn description(&self) -> &str {
""
}
}
impl ParseBlock for CaptureBlock {
fn parse(
&self,
mut arguments: TagTokenIter<'_>,
mut tokens: TagBlock<'_, '_>,
options: &Language,
) -> Result<Box<dyn Renderable>> {
let id = arguments
.expect_next("Identifier expected")?
.expect_identifier()
.into_result()?
.to_string()
.into();
arguments.expect_nothing()?;
let template = Template::new(
tokens
.parse_all(options)
.trace_with(|| format!("{{% capture {} %}}", &id).into())?,
);
tokens.assert_empty();
Ok(Box::new(Capture { id, template }))
}
fn reflection(&self) -> &dyn BlockReflection {
self
}
}
#[derive(Debug)]
struct Capture {
id: kstring::KString,
template: Template,
}
impl Capture {
fn trace(&self) -> String {
format!("{{% capture {} %}}", self.id)
}
}
impl Renderable for Capture {
fn render_to(&self, _writer: &mut dyn Write, runtime: &dyn Runtime) -> Result<()> {
let mut captured = Vec::new();
self.template
.render_to(&mut captured, runtime)
.trace_with(|| self.trace().into())?;
let output = String::from_utf8(captured).expect("render only writes UTF-8");
runtime.set_global(self.id.clone(), Value::scalar(output));
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
use liquid_core::model::Scalar;
use liquid_core::parser;
use liquid_core::runtime;
use liquid_core::runtime::RuntimeBuilder;
fn options() -> Language {
let mut options = Language::default();
options
.blocks
.register("capture".to_string(), CaptureBlock.into());
options
}
#[test]
fn test_capture() {
let text = concat!(
"{% capture attribute_name %}",
"{{ item }}-{{ i }}-color",
"{% endcapture %}"
);
let options = options();
let template = parser::parse(text, &options)
.map(runtime::Template::new)
.unwrap();
let rt = RuntimeBuilder::new().build();
rt.set_global("item".into(), Value::scalar("potato"));
rt.set_global("i".into(), Value::scalar(42f64));
let output = template.render(&rt).unwrap();
assert_eq!(
rt.get(&[Scalar::new("attribute_name")]).unwrap(),
"potato-42-color"
);
assert_eq!(output, "");
}
#[test]
fn trailing_tokens_are_an_error() {
let text = concat!(
"{% capture foo bar baz %}",
"We should never see this",
"{% endcapture %}"
);
let options = options();
let template = parser::parse(text, &options).map(runtime::Template::new);
assert!(template.is_err());
}
}