Use function with or without template

This commit is contained in:
Catalin Constantin Mititiuc 2025-05-21 16:25:13 -07:00
parent 405e19aa72
commit 1803433ae2

View File

@ -73,7 +73,12 @@ template = [[
-- $each{list_posts({limit = 5}), "post"}[[$post]] -- $each{list_posts({limit = 5}), "post"}[[$post]]
-- or like this -- or like this
-- $each{list_posts(), "post"}[[$post]] -- $each{list_posts(), "post"}[[$post]]
list_posts = (args={}) =>
-- can call this two ways. One takes a template, the others uses a template passed
-- in
-- $(list_posts({limit = 2}, true))[[<p>$title</p>]]
-- $each{list_posts({limit = 2}), "post"}[[$post]]
list_posts = (args={}, has_block=false) =>
pages = @site\query_pages { is_a: "post" } pages = @site\query_pages { is_a: "post" }
table.sort pages, (a, b) -> a.source > b.source table.sort pages, (a, b) -> a.source > b.source
limit = math.min args.limit or #pages, #pages limit = math.min args.limit or #pages, #pages
@ -81,11 +86,15 @@ list_posts = (args={}) =>
return for page in *pages[1, limit] return for page in *pages[1, limit]
{ title: title, id: id, publish_date: date, blurb: blurb } = page.meta { title: title, id: id, publish_date: date, blurb: blurb } = page.meta
common.fill_ignoring_pre template, vars = {
title: title or titleize_slug id title: title or titleize_slug id
publish_date: date publish_date: date
blurb: blurb blurb: blurb
url: rootname page\url_for! url: rootname page\url_for!
}
if has_block then vars else common.fill_ignoring_pre template, vars
-- common.fill_ignoring_pre template, vars
-- call like this from a template: $(get_posts())[[<p>$title</p>]] -- call like this from a template: $(get_posts())[[<p>$title</p>]]
-- or like this: $(get_posts({limit = 5}))[[<p>$title</p>]] -- or like this: $(get_posts({limit = 5}))[[<p>$title</p>]]
@ -95,7 +104,6 @@ get_posts = (args={}) =>
limit = math.min args.limit or #pages, #pages limit = math.min args.limit or #pages, #pages
return for page in *pages[1, limit] return for page in *pages[1, limit]
page.target = page.meta.target
{ title: title, id: id, publish_date: date, blurb: blurb } = page.meta { title: title, id: id, publish_date: date, blurb: blurb } = page.meta
{ {
@ -120,6 +128,7 @@ sitegen.create =>
@version = "0.2.12" @version = "0.2.12"
@list_posts = list_posts @list_posts = list_posts
@get_posts = get_posts @get_posts = get_posts
-- @list_posts = (template) -> get_posts -- pass in template here!
add_all = (name="", files) -> add_all = (name="", files) ->
for path, target in pairs files for path, target in pairs files