From 9c2eeaf1f71ad9bee6afb1c7184bee3249765189 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 7 Nov 2018 20:26:33 +0100 Subject: [PATCH] Fewer string cloning for tpl names --- components/library/src/content/section.rs | 10 +++++----- components/library/src/pagination/mod.rs | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index 82c54ee..4c6e14a 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -128,14 +128,14 @@ impl Section { Ok(section) } - pub fn get_template_name(&self) -> String { + pub fn get_template_name(&self) -> &str { match self.meta.template { - Some(ref l) => l.to_string(), + Some(ref l) => l, None => { if self.is_index() { - return "index.html".to_string(); + return "index.html" } - "section.html".to_string() + "section.html" } } } @@ -175,7 +175,7 @@ impl Section { context.insert("current_path", &self.path); context.insert("section", &self.to_serialized(library)); - render_template(&tpl_name, tera, &context, &config.theme) + render_template(tpl_name, tera, &context, &config.theme) .chain_err(|| format!("Failed to render section '{}'", self.file.path.display())) } diff --git a/components/library/src/pagination/mod.rs b/components/library/src/pagination/mod.rs index 47a660e..ab2e8f0 100644 --- a/components/library/src/pagination/mod.rs +++ b/components/library/src/pagination/mod.rs @@ -55,6 +55,7 @@ pub struct Paginator<'a> { pub permalink: String, path: String, pub paginate_path: String, + template: String, /// Whether this is the index section, we need it for the template name is_index: bool, } @@ -73,6 +74,7 @@ impl<'a> Paginator<'a> { path: section.path.clone(), paginate_path: section.meta.paginate_path.clone(), is_index: section.is_index(), + template: section.get_template_name().to_string(), }; paginator.fill_pagers(library); @@ -100,6 +102,7 @@ impl<'a> Paginator<'a> { .clone() .unwrap_or_else(|| "pages".to_string()), is_index: false, + template: format!("{}/single.html", taxonomy.kind.name), }; paginator.fill_pagers(library); @@ -204,22 +207,20 @@ impl<'a> Paginator<'a> { ) -> Result { let mut context = Context::new(); context.insert("config", &config); - let template_name = match self.root { + match self.root { PaginationRoot::Section(s) => { context .insert("section", &SerializingSection::from_section_basic(s, Some(library))); - s.get_template_name() } PaginationRoot::Taxonomy(t) => { context.insert("taxonomy", &t.kind); - format!("{}/single.html", t.kind.name) } }; context.insert("current_url", &pager.permalink); context.insert("current_path", &pager.path); context.insert("paginator", &self.build_paginator_context(pager)); - render_template(&template_name, tera, &context, &config.theme) + render_template(&self.template, tera, &context, &config.theme) .chain_err(|| format!("Failed to render pager {}", pager.index)) } }