8.3 Rendering and Context
| (require camp) | package: camp-lib |
A render context is a table containing information about a page and its context within the current site build. It is provided render functions and provides navigation helpers and metadata.
hash-view
(hash-view context ( slug url collection taxonomies))
slug : string?
url : string?
collection : string?
taxonomies : hash?
The url field contains the canonical URL for the current page. The taxonomies field maps taxonomy keys to lists of normalized values for the current page. Use the prev and next functions with a context to navigate between pages.
8.3.1 HTML Rendering
procedure
(camp-doc->html-xexpr doc [fallback]) → (listof any/c)
doc : document?
fallback : (or/c (-> symbol? list? list? (or/c any/c #f)) #f) = #f
For #lang camp/page documents, calls the body thunk to produce the content. For Punct documents, renders the document content with:
term elements resolved to hyperlinks
term-definition elements rendered as <dfn> with anchors
page-ref elements resolved to page links
Returns a list of x-expressions (the body content without an outer wrapper).
The optional fallback procedure handles custom elements. It receives the tag name, attributes, and rendered child elements. Return an x-expression to override rendering, or #f to use the default (passthrough) behavior.
(define (my-fallback tag attrs elems) (if (eq? tag 'callout) `(aside ((class "callout")) ,@elems) #f)) (define body (camp-doc->html-xexpr doc my-fallback))
8.3.2 Collection Retrieval
struct
(struct page-link (url title metas) #:extra-constructor-name make-page-link #:transparent) url : string? title : string? metas : hash?
procedure
(page-link-doc pl) → document?
pl : page-link?
Must be called within a build context (i.e., when current-site-info is set).
(paginate "blog" #:per-page 5 (λ (items pagination) `(main ,@(for/list ([p items]) `(article (h2 ,(page-link-title p)) ,(camp-doc->html-xexpr (page-link-doc p)))) ,(pagination-nav pagination))))
procedure
(get-collection name [ #:limit limit #:full-docs? full-docs?]) → list? name : string? limit : (or/c #f exact-positive-integer?) = #f full-docs? : boolean? = #f
8.3.3 Taxonomy Functions
procedure
(get-taxonomy-terms collection-name taxonomy-key) → (listof string?) collection-name : string? taxonomy-key : string?
procedure
(get-taxonomy-pages collection-name taxonomy-key [ term]) → (or/c (listof page-link?) hash?) collection-name : string? taxonomy-key : string? term : (or/c string? #f) = #f
8.3.4 Page Navigation
procedure
(prev ctxt [taxonomy-key term]) → (or/c page-link? #f)
ctxt : context? taxonomy-key : string? = #f term : string? = #f
With only ctxt, navigates within the current page’s collection in sort order.
With taxonomy-key, navigates within the pages sharing the current page’s first value for that taxonomy. For example, if the current page has tags: emacs, lisp, then (prev ctxt "tags") navigates within pages tagged "emacs".
With both taxonomy-key and term, navigates within pages having that specific taxonomy term.
(prev ctxt) ; previous in collection (prev ctxt "tags") ; previous in first tag (prev ctxt "tags" "emacs") ; previous in "emacs" tag
procedure
(next ctxt [taxonomy-key term]) → (or/c page-link? #f)
ctxt : context? taxonomy-key : string? = #f term : string? = #f
Calling conventions are the same as prev: with only ctxt, navigates within the collection; with taxonomy-key, navigates within pages sharing the current page’s first value for that taxonomy; with both taxonomy-key and term, navigates within pages having that specific term.
(next ctxt) ; next in collection (next ctxt "tags") ; next in first tag (next ctxt "tags" "emacs") ; next in "emacs" tag
8.3.5 Page Filtering
procedure
(filter-pages pages [ #:date-from date-from #:date-to date-to #:date-key date-key #:taxonomies taxonomies]) → (listof page-link?) pages : (listof page-link?) date-from : (or/c date-provider? #f) = #f date-to : (or/c date-provider? #f) = #f date-key : symbol? = 'date taxonomies : hash? = #hasheq()
#:date-from and #:date-to: Inclusive date bounds. Pages without a date (or with unparseable dates) are excluded when date filtering is active.
#:date-key: The metadata key to use for date comparison (default: 'date).
#:taxonomies: A hash where keys are taxonomy names (strings or symbols) and values are either a single string (exact match) or a list of strings (match any).
This function is useful for building custom page sets or for book parts that filter collections:
(require gregor) ; for date constructor ;; Get 2024 posts tagged "featured" (filter-pages (get-collection "blog") #:date-from (date 2024 1 1) #:date-to (date 2024 12 31) #:taxonomies (hasheq 'tags "featured")) ;; Get posts in any of these categories (filter-pages posts #:taxonomies (hasheq 'category '("tech" "science")))
Taxonomy values in page metadata can be comma-separated strings or lists; both formats are normalized before matching.
8.3.6 Date Formatting
procedure
t : date-provider? pattern : string?
Pattern |
| Output |
| Example |
yyyy |
| 4-digit year |
| 2025 |
yy |
| 2-digit year |
| 25 |
MMMM |
| Full month name |
| January |
MMM |
| Abbreviated month |
| Jan |
MM |
| 2-digit month |
| 01 |
M |
| 1 or 2-digit month |
| 1 |
dd |
| 2-digit day |
| 05 |
d |
| 1 or 2-digit day |
| 5 |
EEEE |
| Full weekday name |
| Wednesday |
EEE |
| Abbreviated weekday |
| Wed |
(~t my-date "MMMM d, yyyy") ; "January 15, 2025" (~t my-date "d MMM yyyy") ; "15 Jan 2025" (~t my-date "yyyy-MM-dd") ; "2025-01-15" (~t my-date "EEE, MMM d") ; "Wed, Jan 15"
See the CLDR documentation for the complete list of pattern symbols.
procedure
pattern : string? v : (or/c string? date-provider?)
;; In a render function: (define date (meta-ref doc 'date)) ; might be "2025-01-15" or a date object (~d "d MMM yyyy" date) ; "15 Jan 2025" - works either way (~d "yyyy-MM-dd" date) ; "2025-01-15" - for datetime attributes
If v is already a date-provider?, it is passed directly to ~t. If v is a string, it is first parsed as an ISO 8601 date.