1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
//! Building HTML views using templates.
//!
//! # Supported template engines
//!
//! The following optional features are available:
//!
//! | Feature flag | Description | Default? |
//! |------------------|------------------------------------------------------|----------|
//! | `view-minijinja` | Enables the `minijinja` template engine. | No |
//! | `view-tera` | Enables the `tera` template engine. | No |
use crate::{application::Application, extension::TomlTableExt};
use std::path::Path;
cfg_if::cfg_if! {
if #[cfg(feature = "view-tera")] {
mod tera;
use self::tera::load_templates;
pub use self::tera::render;
} else {
mod minijinja;
use self::minijinja::load_templates;
pub use self::minijinja::render;
}
}
/// Intializes view engine.
pub(crate) fn init<APP: Application + ?Sized>() {
let app_state = APP::shared_state();
let mut template_dir = "templates";
if let Some(view) = app_state.get_config("view") {
if let Some(dir) = view.get_str("template-dir") {
template_dir = dir;
}
}
let template_dir = if Path::new(template_dir).exists() {
template_dir.to_owned()
} else {
APP::project_dir()
.join("templates")
.to_string_lossy()
.into()
};
load_templates(app_state, template_dir);
}