Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Allow Dynamic Templates use tp.file.include()?

Open rishifter opened this issue 3 years ago • 13 comments

I create notes for films that I watch and use Templater right now to fetch the movie details from the OMDB api and save it all in YAML. Previously, I was saving some of the data as part of the note in a table but now that all my film notes have YAML info (which is much more detailed than before) and with Templater 1.7.0 now, dynamic commands work inside tables which is great! So I plan to have those tables all just fetch YAML data instead and display those. I'm okay with this removing access to the tabular data in edit mode since it has the benefit of avoiding duplication so it's easier in the future to change a spelling mistake for example.

I can already replace all my tables to use <%+ tp.frontmatter.info %> but I realised it'd be better to just replace them with a one liner <%+ tp.file.include("[[Film Template]]") %> and have the actual table formatting with fields I want visible in the file Film Template. This allows me to change the amount of info I want displayed in all the files at one go, so if for example, I wish to display the production company, I just add a row to this file and since the data is in YAML already, all files will automatically show this. Unfortunately, this doesn't seem to be possible right now, unless I'm missing some syntax. When I use it inside a dynamic command, the command fails to render at all. Is it somehow possible to have the same template in multiple files but be able to update it all this way in one go?

rishifter avatar May 24 '21 07:05 rishifter

I also tried to "live include" but to no avail. The idea was to include a [[nearby]] template in location and people notes and have it use the local notes’ front matter. Code re-use, as in your case.

---
nearby: 100 km
---

## Show me

<%+ tp.file.include("[[nearby]]") %>

Moonbase59 avatar May 24 '21 08:05 Moonbase59

I think this should become a feature request instead. But I can't find a way to change the label!

rishifter avatar May 28 '21 09:05 rishifter

I have the same situation. I'm very new to this plugin. I'd be interested to hear if there's a solution for this. Thanks! 🙂

Taitava avatar Oct 03 '21 12:10 Taitava

I defend this. I have set tables via Dataview for people, mocs and such in a way that the tables are reusable by only using the filename.

My intent was to embed the tables via templater dynamic commands and include, so I only have to mantain and update one instance of each table. But to no avail.

Could this become a feature?

veradrawer avatar Dec 18 '21 20:12 veradrawer

Would also love to see this. Loving templater and this would allow me to truly modularise my templates.

simonjbeaumont avatar Jan 08 '22 20:01 simonjbeaumont

Wouldn't "live include" be the same thing as the built in note transclusion: ![[nearby]]?

shabegom avatar Jan 15 '22 21:01 shabegom

Wouldn't "live include" be the same thing as the built in note transclusion: ![[nearby]]?

Maybe in some way, but visually it wraps content in a box. The box is good when something needs to be quoted, but not when something needs to be integrated seamlessly. Another thing I'm not sure of, can it refer to variables that relate to the currently open file, and not to the linked ![[nearby]] file?

Taitava avatar Jan 24 '22 06:01 Taitava

Maybe in some way, but visually it wraps content in a box

There are css snippets out there to change how transclusions show up. This is my fave:

https://gist.github.com/GitMurf/46c9ae78d6c3ce53d42d7832c7601271

Another thing I'm not sure of, can it refer to variables that relate to the currently open file, and not to the linked ![[nearby]] file?

do you have an example use case for this? There are definitely ways to do this, but require using the obsidian api.

shabegom avatar Jan 24 '22 12:01 shabegom

Maybe in some way, but visually it wraps content in a box

There are css snippets out there to change how transclusions show up. This is my fave:

https://gist.github.com/GitMurf/46c9ae78d6c3ce53d42d7832c7601271

I don't think it's clean to transform ![[]] to something else than what it was originally meant for. It's clearly meant for picking content from other files, and prominently showing that the content comes from other files. So, it's a different thing than what is being requested in this issue. If I'd change it with CSS, I guess I could not use it in its original form in cases where I need the box to appear.

Another thing I'm not sure of, can it refer to variables that relate to the currently open file, and not to the linked ![[nearby]] file?

do you have an example use case for this? There are definitely ways to do this, but require using the obsidian api.

I think @Moonbase59 already provided a use case:

I also tried to "live include" but to no avail. The idea was to include a [[nearby]] template in location and people notes and have it use the local notes’ front matter. Code re-use, as in your case.

---
nearby: 100 km
---

## Show me

<%+ tp.file.include("[[nearby]]") %>

How I understood this, is that the idea is to include a file named nearby.md, which accesses the YAML property nearby (I used word variable in my prevous post). I'm not sure what nearby.md would exactly output, but in theory it could output just the value of the nearby property, wrapped in some text. When using a template file to contain the wrapping text, the same text does not need to be repeated in multiple files that include the same template file. And when the wrapping text is changed, it only needs to be changed in one file.

I hope this helped. Maybe @Moonbase59 can explain it even better than me? :slightly_smiling_face:

Taitava avatar Jan 24 '22 14:01 Taitava

How I understood this, is that the idea is to include a file named nearby.md, which accesses the YAML property nearby (I used word variable in my prevous post). I'm not sure what nearby.md would exactly output, but in theory it could output just the value of the nearby property, wrapped in some text. When using a template file to contain the wrapping text, the same text does not need to be repeated in multiple files that include the same template file. And when the wrapping text is changed, it only needs to be changed in one file.

I have pretty much the same use case. I want to include some kind of "live template" in a ever-growing set of notes which renders some common DataView queries. Therefore I need to "overwrite" the file context in the template to be included. This way I can change the DataView queries without updating a lot of notes.

Here is an example:

A note:

# A note

## Backlinks

<%+ tp.file.include("[[dataviewQueries]]") %>

dataviewQueries.md:

'''dataview
LIST
FROM [[]] AND #SomeTag
SORT file.mtime DESC
'''

P.S.: I know that I can show backlinks in document. However, I want to limit/filter the backlinks based on tags, fields etc.

depressiveRobot avatar Jan 24 '22 16:01 depressiveRobot

Yes, this is a good recap. The idea is not to embed pages, but to render a template as-if it was written in the same note. One template to update, instead of every note it was injected to.

veradrawer avatar Jan 26 '22 12:01 veradrawer

Has anyone had any luck querying the frontmatter from another (named) file dynamically? Something along the lines of <%+ tp.file.include("[[nearby]]").frontmatter.x %>

I am trying to set up child templates that can inherit some features from a named parent.

flatlines avatar Aug 28 '22 13:08 flatlines

Nope. Doesn't work that way unfortunately. You can use dataview's API or use app.metadataCache.getFileCache(TFile) to extract frontmatter from other files.

AB1908 avatar Aug 28 '22 14:08 AB1908

Any chance we can see dynamic commands working with the tp.file.include()? this would make my life so much easier!

Ghost-Programmer avatar Feb 15 '23 20:02 Ghost-Programmer