Skip to content

Commit

Permalink
feat: TeraTemplating from_directory & default constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Galitan-dev committed Feb 19, 2023
1 parent f686047 commit 748000b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions poem/src/tera/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ impl TeraTemplatingMiddleware {
Self { tera }
}

/// Create a new instance of TeraTemplating, containing all the parsed
/// templates found in the directory. The errors are already handled. Use
/// TeraTemplating::custom(tera: Tera) to modify tera settings.
///
/// ```no_compile
/// use poem::tera::TeraTemplating;
///
/// let templating = TeraTemplating::from_glob("templates");
/// ```
pub fn from_directory(template_directory: &str) -> Self {
let tera = match Tera::new(&format!("{template_directory}/**/*")) {
Ok(t) => t,
Err(e) => {
println!("Parsing error(s): {e}");
::std::process::exit(1);
}
};

Self { tera }
}

/// Create a new instance of TeraTemplating, using the provided Tera
/// instance
///
Expand All @@ -54,6 +75,12 @@ impl TeraTemplatingMiddleware {
}
}

impl Default for TeraTemplatingMiddleware {
fn default() -> Self {
Self::from_directory("templates")
}
}

impl<E: Endpoint> Middleware<E> for TeraTemplatingMiddleware {
type Output = TeraTemplatingEndpoint<E>;

Expand Down

0 comments on commit 748000b

Please sign in to comment.