SilentVoid13 / Templater

A template plugin for obsidian
https://silentvoid13.github.io/Templater
GNU Affero General Public License v3.0
3.27k stars 198 forks source link

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

Open rishifter opened 3 years ago

rishifter commented 3 years ago

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?

Moonbase59 commented 3 years ago

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]]") %>
rishifter commented 3 years ago

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

Taitava commented 3 years ago

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! 🙂

veradrawer commented 2 years ago

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?

simonjbeaumont commented 2 years ago

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

shabegom commented 2 years ago

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

Taitava commented 2 years ago

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?

shabegom commented 2 years ago

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.

Taitava commented 2 years ago

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:

depressiveRobot commented 2 years ago

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.

veradrawer commented 2 years ago

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.

flatlines commented 2 years ago

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.

AB1908 commented 2 years ago

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.

Ghost-Programmer commented 1 year ago

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