Page list by tag

I use page listings by tag on many pages of my site. I’ve managed to change the code I used in v1 with the template to v2 like this:

${template.each(query[[from index.tag 'the_tag']],
template.new[==[
* [[${name}]]
]==]
)}

I tried to put that code inside a function to reuse it, but I get a parsing error.

function pagelist_by_tag(tag)
  ${template.each(query[[from index.tag tag]],
  template.new[==[
      * [[${name}]]
  ]==]
  )}
end

I think I’m mixing up several different things :sad_but_relieved_face:

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)

You can then use it ${pagelist_by_tag("page")}

Thanks. I’ve understood all your explanations, but I’ve tried the code and have an error:


imagen
imagen

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.

Yes, you are right!

I changed them to single quotes and everything works now.

Thanks :slightly_smiling_face: