I have a template query below and I'd like to call a function that returns the ammount of stars. The sterren() function works fine in a ${sterren(3)} block, but I can't render it within the template query. How do I achieve this?
template query
${template.each(query[[
from index.tag "page"
where name:startsWith("projecten/")
order by created desc limit 5]],
template.new[==[
🏛️ ${icoon} [[${name}]] sterren(${aantal})
]==]
)}
Function:
function sterren(aantal)
if aantal == 1 then tekst = "⭐️" end
if aantal == 2 then tekst = "⭐️⭐️" end
if aantal == 3 then tekst = "⭐️⭐️⭐️" end
if aantal == 4 then tekst = "⭐️⭐️⭐️⭐️" end
if aantal == 5 then tekst = "⭐️⭐️⭐️⭐️⭐️" end
return widget.html(dom.span {
tekst
})
end
type or paste code here
Templates are basically a fancy way to concatenate (markdown) strings. They don't really support widgets. However, for what you want to do, you don't need them, this should work (with some bonus reduced code size and support for an infinite number of sterretjes ):
```space-lua
function sterren(aantal)
return string.rep("⭐️", aantal)
end
```
${template.each(query[[
from index.tag "page"
where name:startsWith("projecten/")
order by created desc limit 5]],
template.new[==[
🏛️ ${icoon} [[${name}]] ${sterren(aantal)}
]==]
)}
tnx @malys . I don't feel comfortable installing this just yet.. I want to grasp my head around silverbullet itself more first. Also, I'm forcing myself to judge if I really-really need this before going the extra mile. Gimme some time
this is cool! I guess the escape here is that I don't need html for the stars. It gets harder when I want to colorize a column word based on its input. I already build a function to do this anywhere within silverbullet, but (as you'd gues) this doesn't work for tables. Would there be a workaround for this too?
function kleurRood(text)
return widget.html(dom.span {
style="background:#7B4760; color: white; padding: 0px 8px; border-radius: 5px;",
text
})
end