How To Reference Frontmatter Properties From Inside Code Block

TL;DR

Is there a way to access/reference the frontmatter/properties of a file from a code block so I can make my templates dynamic?

Backstory

Hey everyone,
I have a bit of a weird yet straightforward problem. I’m trying to use the ‘Fantasy Statblock’ plugin in combination with the ‘Templater’ plugin to make it easier to create a statblock for my D&D characters. However, since the statblock plugin uses a code block to render the output, I can’t seem to find a way to access the frontmatter of the page from the code block. Below is one of the many methods I’ve tried where I’m trying to get the parameter ‘name’ from the frontmatter and have it auto-update in the code block. Does anyone have any idea what I’m doing wrong?
Thanks for taking the time to read this!

Methods Already Tried

```statblock
name: `=this.name`
```

I’ve also tried these formats: <% tp.frontmatter[“name”] %>, {name}, $=dv.current().name

1 Like

I finally was able to get something like this working on an img-gallery block.

If I tried inline direct queries inside of the block, it would appear correct in the preview mode, but the plugin wouldn’t see it. My assumption was an order of operations issue.

The solution I stumbled on was to have dataview generate the entire code block itself at once, so that the plugin cannot try to process it before it’s fully populated.

I’m new to Obsidian, so this took a little while. I know I could have stored the items in an array/dict and iterated over there key/value pairs to reduce the length but I was being lazy.

I have this in my daily-note template. In that, I will get a dataview error because there’s a templater reference in the middle. There’s probably an easier way to do it but I was tired of fighting the issue. This is fine though, as when I create the daily note, that becomes a pure string.

> [!summary]+ **ATTACHMENTS:** 
> ```dataviewjs
> dv.paragraph(
> "```img-gallery\n" + 
> "path: " + <% "\"data/resources/attachments/" + tp.file.folder(true) + "/" + tp.file.title + "/\"" %> + "\n" +
> "type: " + dv.current().galleryType + "\n" +
> "height: " + dv.current().galleryHeight + "\n" +
> "columns: " + dv.current().galleryColumns + "\n" +
> "mobile: " +  dv.current().galleryColumns + "\n" +
> "gutter: 8\n" +
> "radius: 0\n" +
> "sortby: ctime\n" +
> "sort: desc\n" +
> "```\n" 
>)
> ```
> 

I’ve missed this post, but by reading your reply now I’m thinking that this sounds like a task for dv.view(). With a proper dv.view() script to generate that paragraph, your daily notes could have:

> [!summary]+ **ATTACHMENTS:**
> `$= await dv.view("_js/buildImgGallery") `

And then the _js/buildImgGallery.js file could have something like:

const cur = dv.current()
dv.paragraph(`
~~~img-gallery
path: "data/resources/attachments/${ cur.file.folder }/${ cur.file.name }/"
type: ${ cur.galleryType }
height: ${ cur.galleryHeight }
columns: ${ cur.galleryColumns } 
mobile:  ${ cur.galleryColumns }
gutter: 8
radius: 0
sortby: ctime
sort: desc
~~~
`)

The advantage of this being that now if you wanted to change something related to building of the paragraph you could change the dv.view() script, and it immediately would affect all your daily notes. Whereas your current solution if you wanted to change from gutter: 8 to gutter: 10 (whatever that might do :smiley: ) you have to manually change all the daily notes.

Thanks, I appreciate it.

I program mostly in python these days, so my javascript is rusty. I’m trying to learn dataviewjs and obsidian for my personal journal at home on the side in my spare time, so tips like this help. I’ll try to migrate to that approach as I like the modular approach.