Struct handlebars::Context
source · pub struct Context { /* private fields */ }
Expand description
The context wrap data you render on your templates.
Implementations§
source§impl Context
impl Context
sourcepub fn wraps<T: Serialize>(e: T) -> Result<Context, RenderError>
pub fn wraps<T: Serialize>(e: T) -> Result<Context, RenderError>
Create a context with given data
Examples found in repository?
src/registry.rs (line 607)
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
pub fn render<T>(&self, name: &str, data: &T) -> Result<String, RenderError>
where
T: Serialize,
{
let mut output = StringOutput::new();
let ctx = Context::wraps(data)?;
self.render_to_output(name, &ctx, &mut output)?;
output.into_string().map_err(RenderError::from)
}
/// Render a registered template with reused context
pub fn render_with_context(&self, name: &str, ctx: &Context) -> Result<String, RenderError> {
let mut output = StringOutput::new();
self.render_to_output(name, ctx, &mut output)?;
output.into_string().map_err(RenderError::from)
}
/// Render a registered template and write data to the `std::io::Write`
pub fn render_to_write<T, W>(&self, name: &str, data: &T, writer: W) -> Result<(), RenderError>
where
T: Serialize,
W: Write,
{
let mut output = WriteOutput::new(writer);
let ctx = Context::wraps(data)?;
self.render_to_output(name, &ctx, &mut output)
}
/// Render a registered template using reusable `Context`, and write data to
/// the `std::io::Write`
pub fn render_with_context_to_write<W>(
&self,
name: &str,
ctx: &Context,
writer: W,
) -> Result<(), RenderError>
where
W: Write,
{
let mut output = WriteOutput::new(writer);
self.render_to_output(name, ctx, &mut output)
}
/// Render a template string using current registry without registering it
pub fn render_template<T>(&self, template_string: &str, data: &T) -> Result<String, RenderError>
where
T: Serialize,
{
let mut writer = StringWriter::new();
self.render_template_to_write(template_string, data, &mut writer)?;
Ok(writer.into_string())
}
/// Render a template string using reusable context data
pub fn render_template_with_context(
&self,
template_string: &str,
ctx: &Context,
) -> Result<String, RenderError> {
let tpl = Template::compile2(
template_string,
TemplateOptions {
prevent_indent: self.prevent_indent,
..Default::default()
},
)?;
let mut out = StringOutput::new();
{
let mut render_context = RenderContext::new(None);
tpl.render(self, ctx, &mut render_context, &mut out)?;
}
out.into_string().map_err(RenderError::from)
}
/// Render a template string using resuable context, and write data into
/// `std::io::Write`
pub fn render_template_with_context_to_write<W>(
&self,
template_string: &str,
ctx: &Context,
writer: W,
) -> Result<(), RenderError>
where
W: Write,
{
let tpl = Template::compile2(
template_string,
TemplateOptions {
prevent_indent: self.prevent_indent,
..Default::default()
},
)?;
let mut render_context = RenderContext::new(None);
let mut out = WriteOutput::new(writer);
tpl.render(self, ctx, &mut render_context, &mut out)
}
/// Render a template string using current registry without registering it
pub fn render_template_to_write<T, W>(
&self,
template_string: &str,
data: &T,
writer: W,
) -> Result<(), RenderError>
where
T: Serialize,
W: Write,
{
let ctx = Context::wraps(data)?;
self.render_template_with_context_to_write(template_string, &ctx, writer)
}
sourcepub fn data(&self) -> &Json
pub fn data(&self) -> &Json
Return the Json data wrapped in context
Examples found in repository?
src/context.rs (line 178)
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
pub(crate) fn navigate<'reg, 'rc>(
&'rc self,
relative_path: &[PathSeg],
block_contexts: &VecDeque<BlockContext<'reg>>,
) -> Result<ScopedJson<'reg, 'rc>, RenderError> {
// always use absolute at the moment until we get base_value lifetime issue fixed
let resolved_visitor = parse_json_visitor(relative_path, block_contexts, true);
match resolved_visitor {
ResolvedPath::AbsolutePath(paths) => {
let mut ptr = Some(self.data());
for p in paths.iter() {
ptr = get_data(ptr, p)?;
}
Ok(ptr
.map(|v| ScopedJson::Context(v, paths))
.unwrap_or_else(|| ScopedJson::Missing))
}
ResolvedPath::RelativePath(_paths) => {
// relative path is disabled for now
unreachable!()
// let mut ptr = block_contexts.front().and_then(|blk| blk.base_value());
// for p in paths.iter() {
// ptr = get_data(ptr, p)?;
// }
// Ok(ptr
// .map(|v| ScopedJson::Context(v, paths))
// .unwrap_or_else(|| ScopedJson::Missing))
}
ResolvedPath::BlockParamValue(paths, value)
| ResolvedPath::LocalValue(paths, value) => {
let mut ptr = Some(value);
for p in paths.iter() {
ptr = get_data(ptr, p)?;
}
Ok(ptr
.map(|v| ScopedJson::Derived(v.clone()))
.unwrap_or_else(|| ScopedJson::Missing))
}
}
}