Call lua functions from templates

If I have a function that returns a Lua table, is there a way for me to use that table in the regular templates? So this would work?

{{#each ${fetch_rows()}}}
* [[{{ item.web_url } | {{ item.title }}]]
{{/each}}

Or is there another way to render templates? Writing Lua code to combine strings to render is a bit cumbersome.

I haven’t played with space-lua yet but I would expect it’s the same as with space-script function. Once it’s registered, you can call it directly within a template or querry. You only need to put it inside bracket to use it in-line.

Your example would be:

```template
{{#each fetch_rows()}}
* [[{{ item.web_url } | {{ item.title }}]]
{{/each}}
```

From what I can tell that does not work. I don’t think functions are manually registered in Lua. I can register a command (for the command picker), but not a function iiurc.

No you cannot do this and I have little interest in making it work (in the context of Lua, using the template language is legacy). What I envision you do instead is use a Lua expression directive like this (untested, but you get the idea):

${template.each(fetch_rows(), template.new[==[
    * [[${web_url}|${title}]]
]==])

Thanks, this works! In my case it’s a bit tricky, because the title can contain square brackets, and I haven’t quite figured out a nice way to escape those. But this answers my question anyway.

To illustrate, if you are interested, I can’t render these as links, because the extra square brackets throws off the renderer:

function tickets()
  return {
    {title = "[Foo] Foo", url = "https://example.org"},
    {title = "[Bar] Bar", url = "https://example.org"},
  }
end

${template.each(tickets(), template.new[==[
    * [[${url}|${title}]]
]==])}

But when I have more time I just extract the information in the brackets, then render them differently.