mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-19 23:41:22 +02:00
fix: allow for multiple templates directories
This commit is contained in:
@@ -21,19 +21,49 @@ pub enum TemplateEngine {
|
|||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! create_template_engine {
|
macro_rules! create_template_engine {
|
||||||
// Macro takes the relative path to the templates dir as input
|
// Single path argument
|
||||||
($relative_path:expr) => {{
|
($relative_path:expr) => {
|
||||||
|
$crate::create_template_engine!($relative_path, Option::<&str>::None)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Path + Fallback argument
|
||||||
|
($relative_path:expr, $fallback_path:expr) => {{
|
||||||
// Code for debug builds (AutoReload)
|
// Code for debug builds (AutoReload)
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
// These lines execute in the CALLING crate's context
|
// These lines execute in the CALLING crate's context
|
||||||
let crate_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let crate_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
let template_path = crate_dir.join($relative_path);
|
let template_path = crate_dir.join($relative_path);
|
||||||
|
let fallback_path = $fallback_path.map(|p| crate_dir.join(p));
|
||||||
|
|
||||||
let reloader = $crate::utils::template_engine::AutoReloader::new(move |notifier| {
|
let reloader = $crate::utils::template_engine::AutoReloader::new(move |notifier| {
|
||||||
let mut env = $crate::utils::template_engine::Environment::new();
|
let mut env = $crate::utils::template_engine::Environment::new();
|
||||||
env.set_loader($crate::utils::template_engine::path_loader(&template_path));
|
|
||||||
|
let loader_primary = $crate::utils::template_engine::path_loader(&template_path);
|
||||||
|
|
||||||
|
// Clone fallback_path for the closure
|
||||||
|
let fallback = fallback_path.clone();
|
||||||
|
|
||||||
|
env.set_loader(move |name| match loader_primary(name) {
|
||||||
|
Ok(Some(tmpl)) => Ok(Some(tmpl)),
|
||||||
|
Ok(None) => {
|
||||||
|
if let Some(ref fb_path) = fallback {
|
||||||
|
let loader_fallback =
|
||||||
|
$crate::utils::template_engine::path_loader(fb_path);
|
||||||
|
loader_fallback(name)
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => Err(e),
|
||||||
|
});
|
||||||
|
|
||||||
notifier.set_fast_reload(true);
|
notifier.set_fast_reload(true);
|
||||||
notifier.watch_path(&template_path, true);
|
notifier.watch_path(&template_path, true);
|
||||||
|
if let Some(ref fb) = fallback_path {
|
||||||
|
notifier.watch_path(fb, true);
|
||||||
|
}
|
||||||
|
|
||||||
// Add contrib filters/functions
|
// Add contrib filters/functions
|
||||||
$crate::utils::template_engine::minijinja_contrib::add_to_environment(&mut env);
|
$crate::utils::template_engine::minijinja_contrib::add_to_environment(&mut env);
|
||||||
Ok(env)
|
Ok(env)
|
||||||
|
|||||||
Reference in New Issue
Block a user