pub struct Template { /* private fields */ }Expand description
Contains data for Tera template with language mutations.
Implementations§
Source§impl Template
Represent one template. The template has one or more contents. These contents are usually the same but in different
languages. Each content can be assigned only to one language but one language can has more then one contents.ash
impl Template
Represent one template. The template has one or more contents. These contents are usually the same but in different languages. Each content can be assigned only to one language but one language can has more then one contents.ash
Sourcepub fn new(contents: Vec<Content>) -> Result<Self, TemplateError>
pub fn new(contents: Vec<Content>) -> Result<Self, TemplateError>
Create instance from iterable of Content instances or return Err if creation process failed (e.g. name
or language conflict).
Examples found in repository?
examples/fallback_language.rs (line 10)
6fn main() {
7 let mut builder = TerariumBuilder::default();
8 builder.add_template(
9 "my_template".to_owned(),
10 Template::new(vec![Content::new("This is english template, because no czech template is available".to_owned(), vec!["en".to_owned()])]).unwrap()
11 ).unwrap();
12 let terarium = builder.build().unwrap();
13
14 // The EN template will be rendered
15 let result = terarium.render_template(&Context::new(), "my_template", "cs", Some("en")).unwrap();
16 println!("{}", result);
17}More examples
examples/render_single_template.rs (lines 10-13)
6fn main() {
7 let mut builder = TerariumBuilder::default();
8 builder.add_template(
9 "my_template".to_owned(),
10 Template::new(vec![
11 Content::new("This is my template #{{tpl_number}}".to_owned(), vec!["en".to_owned()]),
12 Content::new("Toto je šablona #{{tpl_number}}".to_owned(), vec!["cs".to_owned()]),
13 ]).unwrap()
14 ).unwrap();
15
16 let terarium = builder.build().unwrap();
17
18 let mut ctx = Context::new();
19 ctx.insert("tpl_number", "13");
20
21 let output_en = terarium.render_template(&ctx, "my_template", "en", None).unwrap();
22 let output_cs = terarium.render_template(&ctx, "my_template", "cs", None).unwrap();
23
24 println!("\nEnglish:\n");
25 println!("{}\n", output_en);
26
27 println!("\nCzech:\n");
28 println!("{}\n", output_cs);
29}examples/render_email.rs (lines 10-13)
5fn main() {
6 let mut builder = TerariumBuilder::default();
7
8 builder.add_template(
9 "greet_subject".to_owned(),
10 Template::new(vec![
11 Content::new("Greetings from {{sender}}".to_owned(), vec!["en".to_owned()]),
12 Content::new("Pozdrav od {{sender}}".to_owned(), vec!["cs".to_owned()]),
13 ]).unwrap(),
14 ).unwrap();
15 builder.add_template(
16 "greet_text".to_owned(),
17 Template::new(vec![
18 Content::new("Hello {{username}}".to_owned(), vec!["en".to_owned()]),
19 Content::new("Nazdar {{username}}".to_owned(), vec!["cs".to_owned()]),
20 ]).unwrap(),
21 ).unwrap();
22 builder.add_template(
23 "greet_html".to_owned(),
24 Template::new(vec![
25 Content::new("<p>Hello {{username}}</p>".to_owned(), vec!["en".to_owned()]),
26 Content::new("<p>Nazdar {{username}}</p>".to_owned(), vec!["cs".to_owned()]),
27 ]).unwrap()
28 ).unwrap();
29
30 builder.add_group(
31 "greet_email".to_string(),
32 TemplateGroupBuilder::default()
33 .add_member("subject".to_owned(), "greet_subject".to_owned())
34 .add_member("text".to_owned(), "greet_text".to_owned())
35 .add_member("html".to_owned(), "greet_html".to_owned())
36 .build(),
37 ).unwrap();
38 let terarium = builder.build().unwrap();
39
40 let mut ctx = Context::new();
41 ctx.insert("sender", "Jara Cimrman");
42 ctx.insert("username", "Karel Capek");
43
44 let rendered_group_en = terarium.render_group(&ctx, "greet_email", "en", None).unwrap();
45 let rendered_group_cs = terarium.render_group(&ctx, "greet_email", "cs", None).unwrap();
46
47 println!("\nEnglish");
48 println!("=======\n");
49 rendered_group_en.iter().for_each(|(member_key, content)| println!("{}: {}", member_key, content));
50
51 println!("\nCzech");
52 println!("=====\n");
53 rendered_group_cs.iter().for_each(|(member_key, content)| println!("{}: {}", member_key, content));
54}Sourcepub fn add_content(&mut self, content: Content) -> Result<(), TemplateError>
pub fn add_content(&mut self, content: Content) -> Result<(), TemplateError>
Add new content into template. Return handle of the content.
Sourcepub fn collect_contents(self) -> Vec<Content>
pub fn collect_contents(self) -> Vec<Content>
Collect template content settings as Vec When content has no language, this content is dropped
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnwindSafe for Template
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more