Create inline string variable -> value replacement -- definitions in different vault file (not defined in front-matter of same file)

What I’m trying to do

I’d like to define string-valued variables in a file Y to replace all instances of a variable with its value, inline (not as block etc). This needs to work for any markdown file X in which the variable appears (assume X is child or sibling file of Y).

Example.

Perhaps File Y.? looks like:

var1 : ‘hi’
var2 : ‘bi’
… etc

Perhaps File X.md looks like:

stuff stuff stuff {{var1}} (when raw)
stuff stuff stuff hi (when rendered)

Requirements:

  1. I need a central file from which to define the variables. I.e., The variable assignment doesn’t occur in X.md. (However, importing or referencing the variable in front-matter of X is fine)
  2. Via a simple mechanism, I change the value assigned in Y, this then propagates to its statement in X.

Solutions I’m open to:

  1. Any plugin that can do this reliably.
  2. Any workaround on embeddings which will allow for this to be done via a simple single file Y.
  3. An external program which can point to my vault and accomplish the same thing, so long as the assignments can be accomplished via definitions in a file (of any type) in my vault.

Note: I suspect templater plugin may be able to help here, but a reference to an example in the documentation would be greatly appreciated. Also if this is not a reasonable objective in obsidian, any simple suggestions to accomplish this would be greatly appreciated.

1 Like

@etothepiiminus1 - check out Dataview. It can do what you’re looking for, and is very widely used because it is so versatile.

See the docs regarding inline DQL (DQL = Dataview Query Language) for some hints. You can use traditional frontmatter YAML or inline (inline yaml requires TWO colons), and call it from the same document or from another one.

Here’s a quick mock-up of your example, where the variables are in “File Y” and can be called within “File Y” or from “File X”. In case it isn’t obvious, the top is editing mode, the bottom is reading mode (live preview).

You could easily build one “Variables” file and use the values all through the vault with = [[Variables]].myVariableName

Templater is great (I use it often), but variables are inserted or code is run only when the template is called - AFAIK. I have not tried the other plugin mentioned here, so I can’t comment nor compare.

Good luck!

4 Likes

@etothepiiminus1 I have tried replicating the inline DQL examples as you have shown in my test vault but I am not getting the same results. What settings do you have to have enables to make this functionality work?

1 Like

I think you were asking @Erisred ?
Here’s a partial answer:

  • General good advice: Make sure Dataview is up to date by going to Settings → Community Plugins, “Check for Updates” → “Update All”
  • In Settings → Dataview, check your “Inline Query Prefix”. In @Erisred 's examples it is =. You can either set yours to the same, or change any uses of = in the examples to match whatever you have in the box.

If you are still getting different results, it might be useful to tell us more about what you are getting.

Also be sure to check the dataview settings to enable in-line queries. In pretty sure it’s off by default when the plugin is first installed.

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