Struct handlebars::StringOutput
source · pub struct StringOutput { /* private fields */ }
Implementations§
source§impl StringOutput
impl StringOutput
sourcepub fn new() -> StringOutput
pub fn new() -> StringOutput
Examples found in repository?
More examples
src/registry.rs (line 606)
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
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)
}
src/render.rs (line 543)
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
fn renders<'reg: 'rc, 'rc>(
&'reg self,
registry: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
) -> Result<String, RenderError> {
let mut so = StringOutput::new();
self.render(registry, ctx, rc, &mut so)?;
so.into_string().map_err(RenderError::from)
}
}
/// Evaluate decorator
pub trait Evaluable {
fn eval<'reg: 'rc, 'rc>(
&'reg self,
registry: &'reg Registry<'reg>,
context: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
) -> Result<(), RenderError>;
}
#[inline]
fn call_helper_for_value<'reg: 'rc, 'rc>(
hd: &dyn HelperDef,
ht: &Helper<'reg, 'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
) -> Result<PathAndJson<'reg, 'rc>, RenderError> {
match hd.call_inner(ht, r, ctx, rc) {
Ok(result) => Ok(PathAndJson::new(None, result)),
Err(e) => {
if e.is_unimplemented() {
// parse value from output
let mut so = StringOutput::new();
// here we don't want subexpression result escaped,
// so we temporarily disable it
let disable_escape = rc.is_disable_escape();
rc.set_disable_escape(true);
hd.call(ht, r, ctx, rc, &mut so)?;
rc.set_disable_escape(disable_escape);
let string = so.into_string().map_err(RenderError::from)?;
Ok(PathAndJson::new(
None,
ScopedJson::Derived(Json::String(string)),
))
} else {
Err(e)
}
}
}
}
sourcepub fn into_string(self) -> Result<String, FromUtf8Error>
pub fn into_string(self) -> Result<String, FromUtf8Error>
Examples found in repository?
src/registry.rs (line 609)
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
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)
}
More examples
src/render.rs (line 545)
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
fn renders<'reg: 'rc, 'rc>(
&'reg self,
registry: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
) -> Result<String, RenderError> {
let mut so = StringOutput::new();
self.render(registry, ctx, rc, &mut so)?;
so.into_string().map_err(RenderError::from)
}
}
/// Evaluate decorator
pub trait Evaluable {
fn eval<'reg: 'rc, 'rc>(
&'reg self,
registry: &'reg Registry<'reg>,
context: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
) -> Result<(), RenderError>;
}
#[inline]
fn call_helper_for_value<'reg: 'rc, 'rc>(
hd: &dyn HelperDef,
ht: &Helper<'reg, 'rc>,
r: &'reg Registry<'reg>,
ctx: &'rc Context,
rc: &mut RenderContext<'reg, 'rc>,
) -> Result<PathAndJson<'reg, 'rc>, RenderError> {
match hd.call_inner(ht, r, ctx, rc) {
Ok(result) => Ok(PathAndJson::new(None, result)),
Err(e) => {
if e.is_unimplemented() {
// parse value from output
let mut so = StringOutput::new();
// here we don't want subexpression result escaped,
// so we temporarily disable it
let disable_escape = rc.is_disable_escape();
rc.set_disable_escape(true);
hd.call(ht, r, ctx, rc, &mut so)?;
rc.set_disable_escape(disable_escape);
let string = so.into_string().map_err(RenderError::from)?;
Ok(PathAndJson::new(
None,
ScopedJson::Derived(Json::String(string)),
))
} else {
Err(e)
}
}
}
}