Skip to content

Add ability to add more glob loads without deleting old globs - #1045

Open
tronicboy1 wants to merge 16 commits into
Keats:masterfrom
tronicboy1:master
Open

tronicboy1 wants to merge 16 commits into
Keats:masterfrom
tronicboy1:master

Conversation

@tronicboy1

@tronicboy1 tronicboy1 commented Oct 7, 2026 •

Copy link
Copy Markdown

Changes

  • modifies load_from_glob to be compatible with multiple glob dirs without breaking previous behavior
  • Ensure full_reload is compatible with multiple glob dirs
  • adds add_from_glob, to add multiple glob directories

@Keats

Keats commented Oct 8, 2026

Copy link
Copy Markdown
Owner

What's the usecase?
You can easily load as many as you want via load_from_glob + add template files (still need to handle your own full reload but that's not too hard either) and I haven't had this request since Tera exists so not too inclined on adding it.

@tronicboy1

Copy link
Copy Markdown
Author

I have a project that shares some templates as a base but adds more depending on which deployment it is and what features are enabled. Tera V1 supported my current project structure, now in V2 the same use of glob loading causes it to reset the previous glob load.
add_template_files would mean a lot more work of hard coding directories.

@tronicboy1

Copy link
Copy Markdown
Author

In V1, you could use Tera::extend to run glob multiple times.

// We want to preserve templates that have been added through
        // Tera::extend so we only keep those
        self.templates = self
            .templates
            .iter()
            .filter(|&(_, t)| t.from_extend)
            .map(|(n, t)| (n.clone(), t.clone())) // TODO: avoid that clone
            .collect();

This is how I used it, however in V2 Tera::extend is gone so I cannot use it the same way anymore.

@Keats

Keats commented Oct 15, 2026

Copy link
Copy Markdown
Owner

I think this approach is wrong then. You can change the glob field in Tera to be a Vec and add a new fn like add_from_glob and just iterate on the glob vec for full reload? The approach in this PR is too complex and from a quick lookup it seems buggy since in full_reload in split by , but , is a valid character in a glob eg {html,css}

@tronicboy1

tronicboy1 commented Oct 15, 2026 •

Copy link
Copy Markdown
Author

@Keats
Thank you for your feedback. I considered changing Tera.glob to Vec<String>. As you say that makes more sense.

I changed it here to a Vec and pushed the globs added to it.
13eadcb

By all means if you have a better/more elegant solution I would love it. I just need the ability to add multiple glob directories to restore backwards compatibility with the Tera::extend function.

I think the fact that I am implementing two features in this PR makes it seem too complicated. I am restoring the ability to extend multiple glob dirs, but also adding the ability for load_from_glob to take multiple forms of input. Would it be better to just make add_from_glob accept &str as it's input and remove the code added to globbing.rs for generics?

Comment thread tera/src/tera.rs Outdated
if let Some(glob) = self.glob.clone().as_ref() {
self.load_from_glob(glob)
if self.glob.len() > 0 {
let current_globs = self.glob.clone();

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let current_globs = self.glob.clone();
let current_globs = std::mem::take(&mut self.glob);

We could do it this way so there is no memory alloc cost.

@Keats

Keats commented Oct 16, 2026

Copy link
Copy Markdown
Owner

This is still way too complex. You can probably do it in 20 loc or something. No need to change anything to load_from_glob except pushing it into a vec for example

@tronicboy1

Copy link
Copy Markdown
Author

@Keats
I tried making it as simple as possible, preserving previous behavior while adding ability to push multiple glob dirs. What do you think?

@tronicboy1 tronicboy1 changed the title Allow glob loading from slice of str and add ability to add more glob loads without deleting old globs Add ability to add more glob loads without deleting old globs Oct 16, 2026
@tronicboy1

Copy link
Copy Markdown
Author

@Keats
I fixed the clippy errors. Thank you!

@Keats Keats left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore the outdated comments,

Comment thread tera/src/tera.rs Outdated

// Insert globs already registered
for dir in self.glob.iter() {
all_glob_entries.extend(load_from_glob(dir)?);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will return in case of errors before putting back the prev_templates/globs back

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d900667

@Keats
I addressed this issue and restored the prev_templates functionality.

Comment thread tera/src/tera.rs Outdated
let is_new_and_valid = !glob.is_empty() && self.glob.insert(glob.to_string());

if is_new_and_valid {
all_glob_entries.extend(load_from_glob(glob)?);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5117736

@Keats
I addressed this.

Comment thread tera/src/tera.rs Outdated
/// let mut tera = Tera::default();
/// tera.load_from_glob("examples/basic/templates/**/*").unwrap();
///
/// tera.load_from_glob("examples/dashboard/**/*").unwrap();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change, the basic load_from_glob should not change, the PR should add a function to add globs

@tronicboy1 tronicboy1 Oct 27, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ae21917

@Keats
I added a function add_from_glob that adds a template, and restored the previous behavior of wiping other globs to load_from_glob. I also added a test that ensures the behavior.

Comment thread tera/src/tera.rs Outdated
/// Only used if the `glob_fs` feature is turned on
#[allow(dead_code)]
pub(crate) glob: Option<String>,
pub(crate) glob: HashSet<String>,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a Vec

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Keats
d40e081
I changed the implementation to use a Vec instead of HashMap.

@tronicboy1
tronicboy1 requested a review from Keats October 27, 2026 14:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants