Templater icon indicating copy to clipboard operation
Templater copied to clipboard

tp.file.title not updating when filename changes

Open ghost opened this issue 2 years ago • 9 comments

Plugin informations (please complete the following information):

Expand
  • OS: macOS Big Sur v11.6.2
  • Obsidian version: 1.9.11
manifest.json
{
    "id": "templater-obsidian",
    "name": "Templater",
    "version": "1.9.11",
    "description": "Create and use templates",
    "minAppVersion": "0.11.13",
    "author": "SilentVoid",
    "authorUrl": "https://github.com/SilentVoid13",
    "isDesktopOnly": false
}
data.json
{
  "command_timeout": 5,
  "templates_folder": "tools/templates/main",
  "templates_pairs": [
    [
      "",
      ""
    ]
  ],
  "trigger_on_file_creation": true,
  "auto_jump_to_cursor": false,
  "enable_system_commands": false,
  "shell_path": "",
  "user_scripts_folder": "tools/scripts",
  "enable_folder_templates": true,
  "folder_templates": [
    {
      "folder": "test_folder",
      "template": "tools/templates/main/test_template.md"
    }
  ],
  "syntax_highlighting": true,
  "enabled_templates_hotkeys": [],
  "startup_templates": [
    ""
  ]
}

Describe the bug Template

---
title: "Frontmatter Title"
---

# My Title
tp.file.title: <% tp.file.title %>
tp.config.target_file.basename: <% tp.config.target_file.basename %>

<%* await tp.file.rename("foobar"); _%>

tp.file.title: <% tp.file.title %>
tp.config.target_file.basename: <% tp.config.target_file.basename %>

<%* await tp.file.move("barfoo"); _%>

tp.file.title: <% tp.file.title %>
tp.config.target_file.basename: <% tp.config.target_file.basename %>

Output

---
title: "Frontmatter Title"
---

# My Title
tp.file.title: Untitled
tp.config.target_file.basename: Untitled

tp.file.title: Untitled
tp.config.target_file.basename: foobar

tp.file.title: Untitled
tp.config.target_file.basename: barfoo

Expected behavior I'm expecting tp.file.title to be updated when file is renamed with the rename or move functions.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

ghost avatar Jan 17 '22 17:01 ghost

+1 I am also experiencing the same issue, would be great if this could be patched, TIA.

thegrownupgeek avatar May 11 '22 10:05 thegrownupgeek

You can get around this by storing it in a variable and using that in the other steps.

AB1908 avatar May 11 '22 12:05 AB1908

that works quite well up to a point. I'm including a template which includes a template. In that second template I can't use the variable I declared in the first template.

dredhorse avatar May 23 '22 15:05 dredhorse

In the meantime, in addition to tp.config.target_file.basename we can use this.app.workspace.getActiveFile().basename (Obsidian API) to get the current file title.

welpdx avatar May 29 '22 21:05 welpdx

I'm a dummy- so you'd write this out as:

<% tp.config.target_file.basename + this.app.workspace.getActiveFile().basename %> to get an updateable file title within the note?

Grundle avatar Aug 18 '23 02:08 Grundle

Hey there, I found a solution here at the end of this issue.

It involves using this code snippet :

<% tp.file.rename(tp.date.now() + "_" + tp.config.template_file.basename) %> <% tp.date.now() + "_" + tp.config.template_file.basename %>

The main part being this one :

tp.config.template_file.basename

The rest is merely for :

  • Including a timestamp
  • Renaming to fight obsidian-filename-heading sync

In the end, I simply deactivated filename-heading-sync plugin in the folder I needed to use this code !

mathisgauthey avatar Aug 18 '23 06:08 mathisgauthey

This is the most promising fix I've seen yet, bc it doesn't break the template, but it keeps just pulling the name of the Template on new notes, not the name of the new note with the new renamed date?

Will play a little more with it, but thank you for pointing me in the general direction.

Grundle avatar Aug 18 '23 19:08 Grundle

Exactly ! Don't know about for the other, the only other template I use it for periodic notes such as daily notes : `<% moment(tp.file.title, "YYYY-MM-DD").format("YYYY-MM-DD") %>'

But glad it helped 😁

mathisgauthey avatar Aug 18 '23 19:08 mathisgauthey

嘿嘿,我在 這個問題 的最後找到了一個解決方案。

它涉及使用以下程式碼片段:

<% tp.file.rename(tp.date.now() + "_" + tp.config.template_file.basename) %> <% tp.date.now() + "_" + tp.config.template_file.basename %>

主要部分是這個:

tp.config.template_file.basename

剩下的只是為了:

* 包括時間戳

* 重新命名以對抗黑曜石檔案名稱標題同步

最後,我只是在需要使用此程式碼的資料夾中停用了 filename-heading-sync 外掛!

Thank you, its working

kin30528 avatar Dec 20 '23 08:12 kin30528