Skip to main content

TemplateOptions

Struct TemplateOptions 

Source
pub struct TemplateOptions {
    pub missing_value_policy: MissingValuePolicy,
    pub line_break_policy: LineBreakPolicy,
    pub preserve_whitespace: bool,
    pub headers_and_footers: bool,
    pub footnotes: bool,
    pub filters: FilterRegistry,
}
Expand description

Configuration options for template rendering.

§Examples

use docx_templating::{TemplateOptions, MissingValuePolicy, LineBreakPolicy};

let options = TemplateOptions::default()
    .missing_value_policy(MissingValuePolicy::Empty)
    .line_break_policy(LineBreakPolicy::SplitParagraph)
    .preserve_whitespace(false);

Fields§

§missing_value_policy: MissingValuePolicy

Policy for missing variables.

§line_break_policy: LineBreakPolicy

Policy for newline characters in resolved values.

§preserve_whitespace: bool

Whether to emit xml:space="preserve" on <w:t> elements whose replacement text has leading or trailing whitespace.

When true (the default) the rewriter adds the attribute automatically so Word does not strip significant spaces from the rendered value. Set to false to suppress the attribute entirely, letting Word apply its own whitespace-normalisation rules.

§headers_and_footers: bool

Whether headers and footers are templated.

When false (the default) word/header*.xml and word/footer*.xml are copied into the output unchanged, so a placeholder in a header stays as written.

§footnotes: bool

Whether footnotes and endnotes are templated.

When false (the default) word/footnotes.xml and word/endnotes.xml are copied into the output unchanged.

§filters: FilterRegistry

Filters available to the template.

Starts with the built in text filters upper, lower, trim, default, len and join and the number filters round, fixed, thousands and percent. The dates feature also adds date and timezone. Add or replace a filter with TemplateOptions::filter.

Implementations§

Source§

impl TemplateOptions

Source

pub fn missing_value_policy(self, policy: MissingValuePolicy) -> Self

Set the missing value policy.

Source

pub fn line_break_policy(self, policy: LineBreakPolicy) -> Self

Set the line break policy.

Source

pub fn preserve_whitespace(self, preserve: bool) -> Self

Set whether to preserve edge whitespace in replacement values.

When true (the default) the rewriter emits xml:space="preserve" on <w:t> elements whose replacement text starts or ends with whitespace, preventing Word from stripping those spaces. Set to false to suppress the attribute and let Word normalise whitespace itself.

Source

pub fn headers_and_footers(self, process: bool) -> Self

Set whether headers and footers are templated.

Disabled by default. Enable it when the template puts placeholders in a header or a footer.

§Examples
use docx_templating::TemplateOptions;

let options = TemplateOptions::default().headers_and_footers(true);
assert!(options.headers_and_footers);
Source

pub fn footnotes(self, process: bool) -> Self

Set whether footnotes and endnotes are templated.

Disabled by default. Enable it when the template puts placeholders in a footnote or an endnote.

§Examples
use docx_templating::TemplateOptions;

let options = TemplateOptions::default().footnotes(true);
assert!(options.footnotes);
Source

pub fn filter<F>(self, name: impl Into<String>, filter: F) -> Self
where F: Fn(&Value, Option<&str>) -> Result<Value, FilterError> + Send + Sync + 'static,

Register a filter for {{value | name}} and {{value | name: "arg"}}.

A filter registered here replaces a built in filter with the same name. The closure receives the current value and the optional argument written after a colon, and returns the value for the next filter in the chain.

§Examples
use docx_templating::{FilterError, TemplateOptions};
use serde_json::Value;

let options = TemplateOptions::default().filter("shout", |value, _| {
    let text = value
        .as_str()
        .ok_or_else(|| FilterError::new("value is not a string"))?;
    Ok(Value::String(format!("{}!", text.to_uppercase())))
});

assert!(options.filters.contains("shout"));

Trait Implementations§

Source§

impl Clone for TemplateOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TemplateOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TemplateOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.