Skip to content

Commit

Permalink
Add support for custom extension on folder load (#46)
Browse files Browse the repository at this point in the history
* Load folder with custom file extension preperation

In preperation to support loading files with a custom extension when
loading a folder, the internal function load_folder in
Ramhorns::from_folder, now takes an extension parameter, and have been
extracted as a private associated function of Ramhorns.

* Add support for custom extension on folder load

If a different extension than '.html' is used as file endings on
template files, this adds a function on Ramhorns to specify what that
extension is.

Co-authored-by: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com>
  • Loading branch information
halvko and maciejhirsz committed Jun 16, 2021
1 parent 9541f49 commit f5a29cb
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions ramhorns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ pub struct Ramhorns {

impl Ramhorns {
/// Loads all the `.html` files as templates from the given folder, making them
/// accessible via their path, joining partials as required.
/// accessible via their path, joining partials as required. If a custom
/// extension is wanted, see [from_folder_with_extension]
/// ```no_run
/// # use ramhorns::Ramhorns;
/// let tpls = Ramhorns::from_folder("./templates").unwrap();
Expand All @@ -115,30 +116,51 @@ impl Ramhorns {
pub fn from_folder<P: AsRef<Path>>(dir: P) -> Result<Self, Error> {
let mut templates = Ramhorns::lazy(dir.as_ref())?;

fn load_folder(path: &Path, templates: &mut Ramhorns) -> Result<(), Error> {
for entry in std::fs::read_dir(path)? {
let path = entry?.path();
if path.is_dir() {
load_folder(&path, templates)?;
} else if path.extension().unwrap_or_else(|| "".as_ref()) == "html" {
let name = path
.strip_prefix(&templates.dir)
.unwrap_or(&path)
.to_string_lossy();

if !templates.partials.contains_key(&*name) {
let template = Template::load(std::fs::read_to_string(&path)?, templates)?;
templates
.partials
.insert(name.into_owned().into(), template);
}
Self::load_folder(&templates.dir.clone(), "html", &mut templates)?;

Ok(templates)
}

/// Loads all files with the extension given in the `extension` parameter as templates
/// from the given folder, making them accessible via their path, joining partials as
/// required.
/// ```no_run
/// # use ramhorns::Ramhorns;
/// let tpls = Ramhorns::from_folder_with_extension("./templates", "mustache").unwrap();
/// let content = "I am the content";
/// let rendered = tpls.get("hello.mustache").unwrap().render(&content);
/// ```
pub fn from_folder_with_extension<P: AsRef<Path>>(
dir: P,
extension: &str,
) -> Result<Self, Error> {
let mut templates = Ramhorns::lazy(dir)?;

Self::load_folder(&templates.dir.clone(), extension, &mut templates)?;

Ok(templates)
}

fn load_folder(path: &Path, extension: &str, templates: &mut Ramhorns) -> Result<(), Error> {
for entry in std::fs::read_dir(path)? {
let path = entry?.path();
if path.is_dir() {
Self::load_folder(&path, extension, templates)?;
} else if path.extension().unwrap_or_else(|| "".as_ref()) == extension {
let name = path
.strip_prefix(&templates.dir)
.unwrap_or(&path)
.to_string_lossy();

if !templates.partials.contains_key(&*name) {
let template = Template::load(std::fs::read_to_string(&path)?, templates)?;
templates
.partials
.insert(name.into_owned().into(), template);
}
}
Ok(())
}
load_folder(&dir.as_ref().canonicalize()?, &mut templates)?;

Ok(templates)
Ok(())
}

/// Create a new empty aggregator for a given folder. This won't do anything until
Expand Down

0 comments on commit f5a29cb

Please sign in to comment.