Dataview: inline query inside of a code block

What I’m trying to do

I would like to have something like a variable inside of a code block.

Things I have tried

I think the inline query of the dataview plugin is very useful to dynamically replace placeholders with predefined variables.

To do that, I add the variable and the value to the header with

---
ip_address: "192.168.0.1"
---

and then use the inline query to insert the variable in a specific area of my document: `=this.ip_address`

This works perfectly fine, however if I try to do this inside of a code block, it does not get replaced. For example:

```xml
<my_address>
`=this.ip_address`
</my_address>
```

This is what codeblocks are meant for: they prevent code from being executed.

You could work with a template instead by using the templater plugin. Create the following template:

```xml
<my_address>
<% tp.frontmatter.ip_address %>
</my_address>

As far as I understand, the templater plugin does not fit my needs as it only replaces the placeholder when creating a new note from the template.

In my case, however, I already have a full vault, and there are some specific parts, that need to be changed from time to time - like the IP address.

You can insert this template into an already existing note. Just place the cursor where you want the code to appear and press a hotkey you defined before for that specific template.

The downside, of course, is that you have to do that manually whenever you change the ip address in your frontmatter - the content of the code block won’t be adjusted automatically as would be the case with a dataview query. However, this is the only option I can see so far. As I said, you can’t render anything in a codeblock by their very nature.

Might I ask why you want to present it like that?

Would the following solve your issue?

ip:: 127.0.0.1
`$= "~~~ xml\n<my_address>\n" + dv.current().ip + "\n</my_address>\n~~~\n"`

That does solve my problem. Thank you very much!

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