The forum domain
api.forums manages discussion board categories. Forums can be nested — create sub-forums by passing a parent ID.
Function reference
| Function | Returns | Description |
|---|---|---|
api.forums.get(id) |
table or nil | Get a forum by ID |
api.forums.list(filter?) |
{ items, total } |
List forums with filters |
api.forums.create(name, parentId?) |
table | Create a forum (parentId defaults to 0) |
api.forums.rename(id, name) |
table | Rename a forum |
api.forums.delete(id) |
boolean | Delete a forum |
Creating a forum structure
-- Top-level categories
local general = api.forums.create("General Discussion")
local projects = api.forums.create("Projects")
-- Sub-forums
api.forums.create("Announcements", general.id)
api.forums.create("Introductions", general.id)
api.forums.create("Showcase", projects.id)
api.forums.create("Help Wanted", projects.id)
Listing forums by parent
-- Get top-level forums
local main_forums = api.forums.list({ parent = 0 })
-- Get sub-forums under "General Discussion"
local sub_forums = api.forums.list({ parent = general.id })
Building a forum index page
app:get("/forums", function(ctx)
local categories = api.forums.list({ parent = 0 })
-- For each category, get its sub-forums
for _, cat in ipairs(categories.items) do
cat.children = api.forums.list({ parent = cat.id }).items
end
return ctx:render("forums", { categories = categories.items })
end)
{# forums template #}
{% for cat in categories %}
<div class="category">
<h2>{{ cat.name }}</h2>
{% for sub in cat.children %}
<a href="/forum/{{ sub.id }}">{{ sub.name }}</a>
{% endfor %}
</div>
{% endfor %}
List filter
api.forums.list({
parent = 0, -- filter by parent ID
user = 42, -- filter by creator
search = "general", -- search forum names
order = "id_asc", -- sort order
page = 1,
limit = 50
})
Next: api.posts — content, blog posts, forum threads.