Making aliases using Templater dynamic commands with multiple parameters

What I’m trying to do

I want to make an alias using Templater’s dynamic commands (<%+ ... %>) with multiple parameters, but it doesn’t work well, as I will describe below.

Things I have tried

It works well when the command has only one argument. For example:

---
aliases: <%+ tp.date.now("YYYY-MM-DD") %>
---

(This example might look strange, but it is for an illustration purpose only)

Result (= what I see after switching to the reading mode):

image

However, it doesn’t work when it has multiple arguments. For example:

---
aliases: <%+ tp.date.now("YYYY-MM-DD", 1) %>
---

Result:

It looks like the parser recognizes the comma separating the arguments "YYYY-MM-DD" and 1 as a delimiter separating two aliases. In other words, it appears to interpret the above YAML frontmatter as

---
aliases:
  - <%+ tp.date.now("YYYY-MM-DD"
  - 1) %>
---

I tried enclosing the whole command with quotes as below, but I’ve got the same result.

---
aliases: '<%+ tp.date.now("YYYY-MM-DD", 1) %>'
---

Result:

I also found that this problem doesn’t occur for non-reserved keys
(see here for the list of reserved keys).
For example:

---
key: <%+ tp.date.now("YYYY-MM-DD", 1) %>
---

Result:
image

So I suspect this problem is related to how Obsidian parses the alias metadata.

How can I fix this? Thank you.

Specs

  • macOS Venture (version 13.2)
  • Obsidian version 1.3.5
  • Templater version 1.16.0

In general, using dynamic commands as an alias is a bad idea. If you just use the normal command braces <% ... %>, the alias will be created on file creation and will not shift.

What notes are you creating that you want a shifting alias depending on the day?

Thank you for your reply!

As I wrote, the above example is only for simplifying the explanation.
I thought it would be better not to show what I’m actually working on because it involves my custom user functions and looks a little bit messy, but I show a simplified version below:

---
theorem: 
  type: theorem
  title: Cauchy-Schwarz inequality
  label: cauchy-schwarz-inequality
aliases: 
  - <%+ tp.user.make_latex_alias(tp, tp.frontmatter.theorem.type, tp.frontmatter.theorem.label) %>
  - <%+ tp.user.make_natural_language_alias(tp, tp.frontmatter.theorem.type, tp.frontmatter.theorem.title, 'en', tp.frontmatter.theorem.number) %>
---

I’m making a template for mathematical theorems, definitions, lemmas, and so on. In the frontmatter, it has theorem metadata, which has several sub-metadata:

  • type: specifies the type of the note, such as definition/theorem/lemma/etc
  • title: the title of the theorem
  • label: used to generate a LaTeX-style label (e.g. thm:cauchy-schwarz-inequality) in order to make it easier to convert LaTeX documents that I currently have into markdown, and also to convert markdown to LaTeX in the future

After filling in these metadata, I want to automatically generate aliases using the metadata. In this example, they would be like

  • an alias in the style of LaTeX labels, made from type and label: thm:cauchy-schwarz-inequality
  • a human-friendly alias in English, made from type and title: Theorem (Cauchy-Schwarz inequality)

tp.user.make_latex_alias and tp.user.make_natural_language_alias are my custom user functions to make these aliases.

I want the aliases generated dynamically. In other words,
when I modify the theorem metadata, I want the aliases to change automatically according to the modification that I made to the metadata.
That’s why I need dynamic commands here. If I used the normal commands (<% ... %>), I couldn’t realize such a dynamic behavior.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.