LIQ template: problem with creating new line in widget?

I followed the approach from the documentation to define a custom query and template like following, it is almost perfect except it won’t automatically switch to new line for each of the entry, also I can’t force a linebreak with \n in the template, it is rendered as a explicit \n

Implementation of template

templates = templates or {}

templates.header = template.new[==[- [[${ref}]] \n]==]

Implementation of the widget

function widgets.headerMention(pageName)
  pageName = pageName or editor.getCurrentPage()
  local dailyBasePath = "Journal/Day/" -- Define base path locally

  if not string.startsWith(pageName, dailyBasePath) then
    -- Not a daily note, return empty widget
    return widget.new { markdown = "" }
  end

  local dateStr = string.sub(pageName, #dailyBasePath + 1)

  -- Query for headers matching the date string
  
  local headers = query[[
    from index.tag "header"
    where _.name == dateStr
  ]]

  local md = ""
  if #headers > 0 then
    -- Assuming templates.header exists and formats a single header item
    md = "### Pages Today\n"
       .. template.each(headers, templates.header)
  else
    md = "" -- No matching headers found
  end

  return widget.new {
    markdown = md
  }
end

event.listen {
  name = "hooks:renderBottomWidgets",
  run = function(e)
    -- Render header mentions
    return widgets.headerMention()
  end

render result with explicit \n:

Does it work if you put an extra line in the template itself?

templates = templates or {}

templates.header = template.new([==[- [[${ref}]]
]==])
1 Like

Waow, thanks David. That is a good way to solve it!