Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template behaves unproperly when called recursively #1405

Open
imagebuilder1837 opened this issue Jun 24, 2024 · 1 comment
Open

Template behaves unproperly when called recursively #1405

imagebuilder1837 opened this issue Jun 24, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@imagebuilder1837
Copy link

Plugin information (please complete the following information):

  • OS: Windows 11
  • Templater version: 2.3.3
  • Obsidian version: 1.6.3
  • Templater settings: default

Describe the bug

A template is expected to generate different files for different folders when using it to generate files recursively in folders. But the resulting files are the same.

Expected behavior

Suppose there is a vault like this:

Vault/
├── dir1/
│   ├── dir11/
│   └── dir12/
├── dir2/
│   ├── dir21/
│   └── dir22/
└── template.md

template.md:

Title: <% tp.file.title %>
<%*
var folders = app.vault.getAllLoadedFiles().filter(x => x instanceof tp.obsidian.TFolder);
for (i in folders) {
   var folder = folders[i];
   var file = app.vault.getFileByPath(`${folder.path}/${folder.name}.md`);
   if (folder.parent !== null && file === null) {  // avoid duplicate generation
      tp.file.create_new(tp.file.find_tfile('template'), folder.name, false, folder);
   }
}
-%>

Now use template.md to generate a Untitled.md file in the root directory:

Vault/
├── dir1/
│   ├── dir11/
│   │   └── dir11.md
│   ├── dir12/
│   │   └── dir12.md
│   └── dir1.md
├── dir2/
│   ├── dir21/
│   │   └── dir21.md
│   ├── dir22/
│   │   └── dir22.md
│   └── dir2.md
├── template.md
└── Untitled.md

Results:

Untitled.md:

Title: Untitled

dir1.md dir2.md dir11.md dir12.md dir21.md dir22.md:

Title: dir12

(Expected to be their own titles)

@imagebuilder1837 imagebuilder1837 added the bug Something isn't working label Jun 24, 2024
@McGlear
Copy link

McGlear commented Jul 5, 2024

Wild guess: You are running into trouble because Templater does not create a new tp-object when you call tp.file.create_new, so there are no seperate instances of tp and thus tp.file.title is not scoped differently per file.
However, I do not understand your template to begin with - why call that template file recursively when you are iterating over all folders anyway? What is the purpose of using both the for-loop and recursion?

To achieve what you are trying to do (maybe? possibly? who knows?), why not just use the loop to create the files and parse the content like this:

<%*
var folders = app.vault.getAllLoadedFiles().filter(x => x instanceof tp.obsidian.TFolder);
for (i in folders) {
   var folder = folders[i];
   var file = app.vault.getFileByPath(`${folder.path}/${folder.name}.md`);
   if (folder.parent !== null && file === null) {  // avoid duplicate generation
      tp.file.create_new(`Title: ${folder.name}`, folder.name, false, folder);
   }
}
-%>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants