diff --git a/src/registry.rs b/src/registry.rs index 02301e6fe..c89c34388 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -100,6 +100,24 @@ fn rhai_engine() -> Engine { Engine::new() } +#[cfg(feature = "dir_source")] +pub struct DirectorySourceOptions { + tpl_extension: String, + hidden: bool, + temporary: bool, +} + +#[cfg(feature = "dir_source")] +impl Default for DirectorySourceOptions { + fn default() -> Self { + DirectorySourceOptions { + tpl_extension: ".hbs".to_owned(), + hidden: false, + temporary: false, + } + } +} + impl<'reg> Registry<'reg> { pub fn new() -> Registry<'reg> { let r = Registry { @@ -279,22 +297,23 @@ impl<'reg> Registry<'reg> { /// * `tpl_extension`: the template file extension /// * `dir_path`: the path of directory /// - /// Hidden files and tempfile (starts with `#`) will be ignored. All registered - /// will use their relative name as template name. For example, when `dir_path` is - /// `templates/` and `tpl_extension` is `.hbs`, the file + /// Hidden files and tempfile (starts with `#`) will be ignored by default. + /// Set `DirectorySourceOptions` to something other than `DirectorySourceOptions::default()` to adjust this. + /// All registered templates will use their relative path to determine their template name. + /// For example, when `dir_path` is `templates/` and `DirectorySourceOptions.tpl_extension` is `.hbs`, the file /// `templates/some/path/file.hbs` will be registered as `some/path/file`. /// /// This method is not available by default. /// You will need to enable the `dir_source` feature to use it. /// - /// When dev_mode enabled, like `register_template_file`, templates is reloaded - /// from file system everytime it's visied. + /// When dev_mode is enabled, like with `register_template_file`, templates are reloaded + /// from the file system every time they're visited. #[cfg(feature = "dir_source")] #[cfg_attr(docsrs, doc(cfg(feature = "dir_source")))] pub fn register_templates_directory
(
&mut self,
- tpl_extension: &str,
dir_path: P,
+ options: DirectorySourceOptions,
) -> Result<(), TemplateError>
where
P: AsRefHallo {{world}}!
").unwrap();
- r.register_templates_directory(".hbs", dir.path()).unwrap();
+ r.register_templates_directory(dir.path(), DirectorySourceOptions::default())
+ .unwrap();
assert_eq!(r.templates.len(), 3);
assert_eq!(r.templates.contains_key("t1"), true);
@@ -878,7 +907,8 @@ mod test {
let mut file3: File = File::create(&file3_path).unwrap();
writeln!(file3, "Hello world!
").unwrap();
- r.register_templates_directory(".hbs", dir.path()).unwrap();
+ r.register_templates_directory(dir.path(), DirectorySourceOptions::default())
+ .unwrap();
assert_eq!(r.templates.len(), 4);
assert_eq!(r.templates.contains_key("t4"), true);
@@ -913,7 +943,8 @@ mod test {
let mut file3: File = File::create(&file3_path).unwrap();
writeln!(file3, "Ciao {{world}}!
").unwrap();
- r.register_templates_directory(".hbs", dir.path()).unwrap();
+ r.register_templates_directory(dir.path(), DirectorySourceOptions::default())
+ .unwrap();
assert_eq!(r.templates.len(), 7);
assert_eq!(r.templates.contains_key("french/t7"), true);
@@ -941,7 +972,8 @@ mod test {
if !dir_path.ends_with("/") {
dir_path.push('/');
}
- r.register_templates_directory(".hbs", dir_path).unwrap();
+ r.register_templates_directory(dir_path, DirectorySourceOptions::default())
+ .unwrap();
assert_eq!(r.templates.len(), 8);
assert_eq!(r.templates.contains_key("t10"), true);
@@ -965,8 +997,14 @@ mod test {
if !dir_path.ends_with("/") {
dir_path.push('/');
}
- r.register_templates_directory(".hbs.html", dir_path)
- .unwrap();
+ r.register_templates_directory(
+ dir_path,
+ DirectorySourceOptions {
+ tpl_extension: ".hbs.html".to_owned(),
+ ..Default::default()
+ },
+ )
+ .unwrap();
assert_eq!(r.templates.len(), 1);
assert_eq!(r.templates.contains_key("t11"), true);
@@ -974,6 +1012,33 @@ mod test {
drop(file1);
dir.close().unwrap();
}
+
+ {
+ let dir = tempdir().unwrap();
+ let mut r = Registry::new();
+
+ assert_eq!(r.templates.len(), 0);
+
+ let file1_path = dir.path().join(".t12.hbs");
+ let mut file1: File = File::create(&file1_path).unwrap();
+ writeln!(file1, "Hello {{world}}!
").unwrap();
+
+ r.register_templates_directory(
+ dir.path(),
+ DirectorySourceOptions {
+ hidden: true,
+ ..Default::default()
+ },
+ )
+ .unwrap();
+
+ assert_eq!(r.templates.len(), 1);
+ assert_eq!(r.templates.contains_key(".t12"), true);
+
+ drop(file1);
+
+ dir.close().unwrap();
+ }
}
#[test]