How to Execute a Codeblock Inside DataviewJs?

So, I know it’s possible to execute dataview query within a query with dv.execute() , but is it possible to execute a different kind of code block?

For example, a code block type used by a different plug-in, like a page-gallery code block?

It depends on when and where the other codeblock is parsed and executed.

So the easiest option is simply to try it out, and see what happens. This thread shows an example of doing a Tasks code block from within Dataviewjs.

Personally I often use this at the end of my dataviewjs:

else
  dv.paragraph(`~~~\n${ result.error }\n~~~`)

This showcases three aspects:

  • First of all the usage of tildes instead of backticks which is perfectly legal for code block fences. It saves you a whole lot of escaping backticks
  • The usage of dv.paragraph() to output the code block back to the page
  • The usage of ${ result.error } to present a random variable within the output

So you could try something like the following:

```dataviewjs
dv.paragraph(`
~~~page-gallery
... here goes your gallery stuff ...
~~~
`)
```

And if you need to do other javascript stuff in front, please do so, and if you want that listed within the code block, use something like: ${ myVariable }.

1 Like

Wow, thank you so much for your help! I never thought about trying to render it with a simple dv.paragraph function, but it makes sense now that I realize that the output is always treated like Markdown. I tried it and it worked! Now I can program my pages to display dataview tables on desktop and page gallery views on mobile :smile:

1 Like

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