Where to put tags in Dataview? YAML or BODY?

Things I have tried

testing

What I’m trying to do

sort by tag

took a break from dataview, but now going back, I’m a bit confused about something. I thought that when I choose what tags i want to see notes from, e.g.

TABLE Name, Birthplace, Years, Tag
FROM #cat/history 
SORT Name DESC

that the tags had to be in the YAML section, but experimenting, I’m seeing that it doesn’t seem to matter whether the tag is in YAML or the body of the note. I would prefer the body of the note. Before I take them out of YAML, wanted to be sure that I’m not missing something or would have some loss of functionality in another unforseen case.

Also, seems there is some standard YAML tag formatting, but also testing, seems multiple things work. eg. w/ and w/o brackets, w/ and w/o quotes, commas, etc.

Is there a definitive way to write tags?

e.g.

Tags: [" #note/type/markup #note/source/blog-newsletter #note/source/mia #topic/american-politics "] (sometimes I dont have the bracket, and sometimes I have commas in there)

Tag:

  • " #note/type/markup"
  • " #note/source/blog-newsletter"
  • " #topic/american-politics"
2 Likes

Hi romebot! No, Obsidian permits tags both in the YAML (e.g. tags: todo) and in the body (e.g. #todo). Both methods work fine, and which you use is up to your personal preference. Dataview can see tags in either place too.

1 Like

There is a subtle nuance as to the placement of tags depending your usage and patience. If placed inline, that particular block of text around the tag shows up in search results instead of just a barren tag, at the top of the note or above or below the associated block. I find, in my usage, that if a note is atomic like, a tag works ok at the top. If a note is large with many different disparate blocks of text, it reduces search friction to place the tag inline (actually I like to put them at the end of a block).

3 Likes

thanks so much! couple followup q’s: does it matter if you write tag or tags? and what about the various formats I described w/ and w/o quotes, commas, and bracket?

cheers!

1 Like

thanks! could you expound on that maybe w/ an example… seems like it might be relevant to what I’m doing, but can’t visualize what you mean. best,

R

p.s. did it used to be different? I feel like I remember reading that for dataview to work it had to be in YAML. So I was putting it in both places so I could use the normal tag functionality. I’m happy to hear I can just use them in the body however!

Yeah, great question. It has to be valid YAML or else the frontmatter won’t parse properly. But apart from that Obsidian is very forgiving. The following all work for me:

tag: one, two, three
tags: one, two, three
tags: ["#one", "#two", "#three"]
tags:
- one
- two
- three

A rule of thumb: Don’t to use the # symbol in the metadata unless you enclose the tag in quotes.

3 Likes

I’m not certain. I seem to remember in the early days of Dataview it was only able to see fields in the YAML frontmatter, but soon after inline fields became available (e.g. Fieldname:: Value). But I think maybe tags always worked with Dataview no matter where they were in the doc? That’s my best guess. :slight_smile:

2 Likes

Yes, @Craig is right. If valid tags for Obsidian, then valid tags for Dataview (anywhere).

1 Like

thanks so much all!

actually, one more question. Same as the conversation about where to put tags. what’s the difference if I put e.g. the author in YAML in quotes, vs. a link. how does this affect the dataview display, the map, linking, or anything else? Is there a difference? It’d be great not to put this info in twice (in quotes in YAML and link in body) and in this case, I’d rather put it as a link in the YAML if it makes no difference.

e.g.

---
Author: [[Patricia Highsmith]]
---

vs

---
Author: "Patricia Highmith"
---

my dataview code is something like this:

TABLE Author, Year, Type, Genre, POV, Length, Summary, Note, Status
Where Type="Novel"
SORT Title DESC

seems to return the author no matter which way I do it, but wondering as I stated above if there any downsides to putting a page link in YAML vs. body.

cheers!

1 Like

Great question! They’re completely different:

Author:: [[Name]] is a link to a page. On a report, Dataview will display this as a clickable link. In a query, the page’s metadata is available too, e.g. Author.link.

Author:: "Name" is just a string of letters. On a report, Dataview will display this as plain text. There’s no association with a page in the vault.

2 Likes

Adding two more things (in addition to what @Craig said):

  1. Links in yaml need the quotation marks
---
Author: "[[Patricia Highsmith]]"
---
  1. If graph it’s an important tool to you, links in yaml don’t show in graph. Links in in-line fields, besides a value in a specific field, they’re an implicit field in page (outlinks).
3 Likes

thanks for that. It works now w/ the quotes, but could you explain more the following: “Links in in-line fields, besides a value in a specific field, they’re an implicit field in page (outlinks).” would I then need to put the author link also in the body if I want it in the graph? is the only advantage in yaml is that the dataview display provided a clickable link?

thanks! but what does this mean: the page’s metadata is available too, e.g. Author.link.

Sure. Maybe an example shows it best:

In your page content, if you add tags or backlinks they becomes automatically page metadata (in dataview language, “implicit fields”). That’s why you have that information in tag/backlinks/outgoing links panes and in graph.
All these are page-level metadata.
If you want to associate this data with a specific field you can. For example:

author:: [[Patricia Highsmith]]

In this case, you have the value [[Patricia Highsmith]] in two fields:

  1. the author field;
  2. the implicit field file.outlinks.

The practical difference is: if the link is an non-existent file, then dataview don’t read it as an outlink (although it appears in outgoing links pane and in graph).

Going back…

If you use an yaml frontmatter field as

author: "[[Patricia Highsmith]]"

then you’re working only in the created field author: isn’t an outlink neither display in graph.

One last “extra”: if you want to look to the structure of the metadata in a specific note, use this inline query:

`=this`

For a more structured display of the metadata, use instead this one (you need to allow inline js queries in settings > dataview):

`$=dv.span(dv.current())`
3 Likes

thanks for this, some of it is above my paygrade at the moment, so will have to take my time digging into it.

followup question. I discovered that if I put my tags in YAML, they don’t show up in the tag pane, so I need them in the body. But seems like when using dataview and indicating FROM, it still works if the tag is in the body, so that’s good. is this correct?

I don’t think that’s true. Tags correctly defined in the YAML should show up in the tag pane (and they do for me).

It’s possible there’s a problem with the way you’re putting tags in the YAML. Would you be interested in posting an example of a YAML block where you’d defined some tags that aren’t showing up in the tag page?