From a0e6e3756bb68b10bd796e1af0276e188e9bff7e Mon Sep 17 00:00:00 2001 From: The-EDev Date: Fri, 18 Mar 2022 13:55:27 +0300 Subject: [PATCH] added set_global_base method to change the default mustache templates directory --- include/crow/app.h | 1 + include/crow/mustache.h | 18 ++++++++++++++++++ include/crow/routing.h | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/crow/app.h b/include/crow/app.h index c0a9a854c..39de350d0 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -179,6 +179,7 @@ namespace crow } /// Set a response body size (in bytes) beyond which Crow automatically streams responses (Default is 1MiB) + /// /// Any streamed response is unaffected by Crow's timer, and therefore won't timeout before a response is fully sent. self_t& stream_threshold(size_t threshold) diff --git a/include/crow/mustache.h b/include/crow/mustache.h index e69be72e6..f77f0775c 100644 --- a/include/crow/mustache.h +++ b/include/crow/mustache.h @@ -631,6 +631,13 @@ namespace crow static std::string template_base_directory = "templates"; return template_base_directory; } + + /// A base directory not related to any blueprint + inline std::string& get_global_template_base_directory_ref() + { + static std::string template_base_directory = "templates"; + return template_base_directory; + } } // namespace detail inline std::string default_loader(const std::string& filename) @@ -668,6 +675,17 @@ namespace crow } } + inline void set_global_base(const std::string& path) + { + auto& base = detail::get_global_template_base_directory_ref(); + base = path; + if (base.back() != '\\' && + base.back() != '/') + { + base += '/'; + } + } + inline void set_loader(std::function loader) { detail::get_loader_ref() = std::move(loader); diff --git a/include/crow/routing.h b/include/crow/routing.h index be4b2ad89..4d35e45fc 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -595,8 +595,8 @@ namespace crow { if (!custom_templates_base.empty()) mustache::set_base(custom_templates_base); - else if (mustache::detail::get_template_base_directory_ref() != "templates") - mustache::set_base("templates"); + else if (mustache::detail::get_template_base_directory_ref() != mustache::detail::get_global_template_base_directory_ref()) + mustache::set_base(mustache::detail::get_global_template_base_directory_ref()); detail::routing_handler_call_helper::call< detail::routing_handler_call_helper::call_params,