How can I add this type of query to a page template?

So, I'm very much on board with the incremental formalization way of working, and I've been using it for a while now.

However, I want to step it up a little, and I'm unable to figure out how to get the result I'm looking for.

What I have:

  • various pages tagged "person", describing, you guessed it, persons.
  • a person may belong to an organisation (which gets its own page, so far I'm still 100% following the guide)
  • a person may belong to one or more teams (which in turn may belong to an organisation)
  • teams and organisations are listed as links to the relevant pages, just like in the guide.

For now, I'm hand-rolling queries for each team and organisation page to list all people within that team or organisation. The system works, in that I can click around and view graphs and everyone who sees it is suitably impressed. However, manually adding stuff to pages doesn't scale that well, so I wanted to make things better with templates. I envisioned having this team template, that automatically adds the correct snippet of query magic to populate it with people, and an organisation template to do the same for organisations.

What I can't figure out, however, is how to get that query into the template, and actually working in the resulting page. In fact, I can't even get it to work outside a template. If I add this snippet:

${query[[
  from p = index.contentPages("person")
  where (table.includes(p.teams, "[[${editor.getCurrentPage()}]]"))
  select templates.pageItem(p)
]]}

...I get an emptly list. If I replace the editor.getCurrentPage() bit with a string like "[[Team/MyAwesomeTeam]]", it'll work.

So I think step one is figuring out how to have the query contain the dynamic bit to look up. Next step would be encoding that in a template, but I think using the ${"$"}{query...} syntax should do the trick there, as described in this thread.

Can anyone help me, or point me towards the bit of documentation I might've missed? Will this work at all, or should I look into widgets or whatnot?

This stuff always get mind bendy, but I think you want the current page check to actually land in the end page so you shouldn’t evaluate it during instantiation. Something like this?

---
tags: meta/template/page
…
---
# Members

${"$"}{query[[
  from p = index.contentPages("person")
  where table.includes(p.teams, "[[" .. editor.getCurrentPage() .. "]]")
  select templates.pageItem(p)
]]}

This reminds me that I really need to optimize the query engine a bit... In queries like this, it looks up the currently open page in the editor for EVERY page object. Ok if you just have a few, but gets expensive fast. I have an idea how to fix this, though. Good reminder :slight_smile:

I could have sworn that I had tried that combination, but apparently not. This works. Thanks a lot!