Page template from new ref

I've got a page template for persons, which I use with a command button. Each person has its own subpage. I ref persons in notes by typing [[persons/mike]]

But sometimes i need to create a new person super quick while in a note. If I ref a person that doesn't exist, that creates a new empty page in /person. However that new page is blank instead of populated by a person template.

Q: is it possible to run a person template when a new page is created? For example triggering because it's in a sub folder of /person? Alternative solutions are also welcome :).

You could check out and customize this script for you use case:

:warning: NOTE :warning:
This script doesn't use the Silverbullet Page Templates, but a simple page with functions in it, so you don't need to add the "special" frontmattr to the template, just the things you want the page to be prepopulatd with.

I need some time and a pc to grasp this, but thanks again @Mr.Red. You're on fire

it is as simple as this:

copy/paste this into a space-lua codeblock

event.listen {  
  name = "editor:pageCreating",  
  run = function(e)  
    if not e.data.name:startsWith("persons/") then return end  
    local rawTemplate = space.readPage("Library/Custom/New_Person_Template")  
    local tree = markdown.parseMarkdown(rawTemplate)  
    local expandedTree = markdown.expandMarkdown(tree)  
    local template = markdown.renderParseTree(expandedTree)  
    return { text = template, perm = "rw" }  
  end  
}

and create a template page like this for example Library/Custom/New_Person_Template:

---
name: "|^|"
phone: ""
birthday: ""
image_url: ""
---

# Hobbies
- 
-
...

In the template’s text, you can optionally use |^| as a placeholder for where the cursor should be after creating the page.

then if you click on a non-existing/aspiring page like [[persons/mike]] it will create the page with the contents from the template prefilled

1 Like