You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
733B

  1. use tera::{Tera, Context};
  2. use errors::Result;
  3. /// Renders the given template with the given context, but also ensures that, if the default file
  4. /// is not found, it will look up for the equivalent template for the current theme if there is one
  5. pub fn render_template(name: &str, tera: &Tera, context: &Context, theme: Option<String>) -> Result<String> {
  6. if tera.templates.contains_key(name) {
  7. return tera
  8. .render(name, context)
  9. .map_err(|e| e.into());
  10. }
  11. if let Some(ref t) = theme {
  12. return tera
  13. .render(&format!("{}/templates/{}", t, name), context)
  14. .map_err(|e| e.into());
  15. }
  16. bail!("Tried to render `{}` but the template wasn't found", name)
  17. }