templater: add Template<C> adapter that evaluates Template<()> with value of C

The idea is basically the same as list.map(|x| ...) template. It compiles the
inner template with a placeholder variable of type 'C', and evaluate it for
each instance variable by injecting the variable through RefCell.
This commit is contained in:
Yuya Nishihara 2024-03-19 20:10:59 +09:00
parent 017026148b
commit 7b641c0236

View file

@ -617,6 +617,28 @@ impl<C, O: Clone> TemplateProperty<C> for PropertyPlaceholder<O> {
}
}
/// Adapter that renders compiled `template` with the `placeholder` value set.
pub struct PlaceholderTemplate<C, T> {
template: T,
placeholder: PropertyPlaceholder<C>,
}
impl<C: Clone, T: Template<()>> PlaceholderTemplate<C, T> {
pub fn new(template: T, placeholder: PropertyPlaceholder<C>) -> Self {
PlaceholderTemplate {
template,
placeholder,
}
}
}
impl<C: Clone, T: Template<()>> Template<C> for PlaceholderTemplate<C, T> {
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
self.placeholder
.with_value(context.clone(), || self.template.format(&(), formatter))
}
}
pub fn format_joined<C, I, S>(
context: &C,
formatter: &mut dyn Formatter,