The template syntax, i.e. ${ ... } only works inside of the actual page (it’s a markdown thing) and what it actually does is evaluate the code you put in it and render the returned stuff in a sensible way. In this case template.each takes the template (which is actually just a function, doesn’t matter here) and applies that to each object in a table, once done it concatenates them into a big string and returns that. That’s the string that’s ultimately shown to you.
If you now want to wrap your example in a function you just have to pass the string on, that template.each generates. So it would look like this
function pagelist_by_tag(tag)
return template.each(query[[from index.tag(tag)]],
template.new[==[
* [[${name}]]
]==]
)
end
(note you will have to put parens around the tag variable, because lua only allows the syntax without the parens for literals)
Those are the wrong quotes I think. Those are the “smartquotes”. You have to be careful with that, if you type a quote inside a luawidget or code block those shouldn’t be created, silverbullet only replaces the normal quotes with smartquotes outside of the “coding” contexts.