There is another approach to page templates and that’s to use the new editor:pageCreating
event, which is triggered when the user navigates to a page that does not yet exist:
event.listen {
name = "editor:pageCreating",
run = function(e)
if not e.data.name:startsWith("Journal/") then
return
end
return {
text = "Template text here",
perm = "rw"
}
end
}
When responding to this event, the text
attribute will be used as the default page text. This means that if you now navigate to any non-existing page that starts with Journal/
, the “Template text here” text will automatically be pre-filled.
Now another fun aspect of this feature is that you can also return ro
as the perm
, which will turn the page into a read-only page. And guess what, now we have an implementation of Virtual Page Templates
This is how tag pages are implemented in v2: Library/Std/Tag when you navigate to a page starting with tag:
the tag template will kick in. Example: tag:meta (this replaces the previous built-in 📌 meta pages from v1).