On this page:
context
9.3.1 HTML Rendering
camp-doc->html-xexpr
9.3.2 Collection Retrieval
page-link
page-link?
page-link-url
page-link-title
page-link-metas
page-link-date
page-link-updated
page-link-doc
get-collection
9.3.3 Taxonomy Functions
get-taxonomy-terms
get-taxonomy-pages
9.3.4 Page Navigation
prev
next
9.3.5 Page Filtering
filter-pages
9.3.6 Static File URLs
static-url
9.3.7 Additional Output Files
current-output-dir
9.3.8 Date Formatting
~t
~d
9.3

9.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
    [source #:default ....]))
  slug : string?
  url : string?
  collection : string?
  taxonomies : hash?
  source : (or/c string? #f)
A hash-view representing a render context. The context is passed to render functions and provides access to taxonomy data.

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.

The source field is the path of the page’s source file, relative to the site root (the folder containing the camp/site file), with "/" as the separator on every platform: for example, "blog/hello-world.md.rkt". It does not depend on the page’s slug, so it is suitable for building links to the file in a version control web interface. Every page generated by a paginated camp/page document has the same source. Contexts constructed by other code may omit it, in which case context-source returns #f.

9.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
Renders a Punct document to HTML x-expressions with cross-references resolved. This is the primary function for converting document bodies in render functions.

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))

9.3.2 Collection Retrieval🔗ℹ

procedure

(page-link url title metas) → page-link?

  url : string?
  title : string?
  metas : hash?
Returns a lightweight reference to a page, used for navigation and collection retrieval. Contains the page’s URL, title, and full metadata hash.

The page’s date and updated metas are parsed once, here, in the site timezone (see Dates and time zones). The metas hash keeps them as written.

Raises exn:fail:contract if either value cannot be parsed, or if updated is earlier than date.

As a match pattern, page-link takes either the three fields above or all five, adding the page-link-date and page-link-updated values:

(match pl
  [(page-link url title _ #f _) `(a ((href ,url)) ,title)]
  [(page-link url title _ date _) `(a ((href ,url)) ,title " " ,(~t date "yyyy"))])

Changed in version 1.1 of package camp-lib: Parses and checks the date and updated metas, and accepts five fields as a match pattern. No longer the name of a structure type: forms such as struct-copy and struct-out do not accept it.

procedure

(page-link? v) → boolean?

  v : any/c
Returns #t if v is a page link.

procedure

(page-link-url pl) → string?

  pl : page-link?

procedure

(page-link-title pl) → string?

  pl : page-link?

procedure

(page-link-metas pl) → hash?

  pl : page-link?
Return the values given to page-link.

procedure

(page-link-date pl) → (or/c moment? #f)

  pl : page-link?
Returns the page’s date meta as a moment, or #f if the page has no date.

Added in version 1.1 of package camp-lib.

procedure

(page-link-updated pl) → (or/c moment? #f)

  pl : page-link?
Returns the page’s updated meta as a moment. If the page has a date but no updated, returns the same value as page-link-date. If the page has no date, returns #f.

Added in version 1.1 of package camp-lib.

procedure

(page-link-doc pl) → document?

  pl : page-link?
Retrieves the full Punct document for a page-link. This allows paginated pages to display full post content rather than just titles and metadata.

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?    
  #:include-drafts? include-drafts?]) → list?
  name : string?
  limit : (or/c #f exact-positive-integer?) = #f
  full-docs? : boolean? = #f
  include-drafts? : any/c = #f
Retrieves pages from a named collection. Returns a list of page-link structs by default, or full Punct documents if full-docs? is #t. The optional limit restricts the number of results returned.

Pages whose metadata contains a draft? key with any non-#f value are excluded by default. Pass a true value for include-drafts? to include them. The limit is applied after draft filtering.

9.3.3 Taxonomy Functions🔗ℹ

procedure

(get-taxonomy-terms collection-name    
  taxonomy-key) → (listof string?)
  collection-name : string?
  taxonomy-key : string?
Returns all distinct values for a taxonomy within a collection, ordered by first appearance in the collection’s sort order.

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
With two arguments, returns a hash mapping each taxonomy term to its list of page-links. With three arguments (including a specific term), returns the list of page-links for that term.

9.3.4 Page Navigation🔗ℹ

procedure

(prev ctxt [taxonomy-key term]) → (or/c page-link? #f)

  ctxt : context?
  taxonomy-key : string? = #f
  term : string? = #f
Returns the previous page relative to the current page, or #f if there is no previous page.

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
Returns the next page relative to the current page, or #f if there is no next page.

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

9.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()
Filters a list of page-links by date range and/or taxonomy values. All filters are combined with AND logic—pages must pass all specified filters to be included.

  • #:date-from and #:date-to: Inclusive date bounds, compared by calendar date only. Pages without a date are excluded when date filtering is active, and an unparseable date raises an exception (see Dates and time zones).

  • #:date-key: The metadata key to use for date comparison (default: 'date). With 'updated, a page that has no updated value is compared by its date, as for page-link-updated.

  • #: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.

Changed in version 1.1 of package camp-lib: With #:date-key 'updated, a page that has no updated value is compared by its date, and a page that has no date is excluded. Before, only the updated value counted.

9.3.6 Static File URLs🔗ℹ

procedure

(static-url path) → string?

  path : string?
Returns the URL of a file in the site’s static folder, with a fingerprint of the file’s contents added to the filename. The path is relative to the static folder; a leading / is optional. It must otherwise name the file exactly as the static folder does: the same letter case, / between elements, and no . or .. elements.

(static-url "/css/style.css") ; => "/css/style-a4a3b627.css"

The fingerprint is the first eight hexadecimal digits of the SHA-1 hash of the file, inserted before the last extension. Because the URL changes whenever the contents do, browsers and CDNs can cache the file indefinitely and still fetch a new copy as soon as it changes.

Use it in a render function wherever the URL of a static file is called for:

(define (render-page doc ctx)
  `(html
    (head (link ((rel "stylesheet") (href ,(static-url "/style.css")))))
    (body ,@(camp-doc->html-xexpr doc))))

Any file in the static folder can be given to static-url, not only stylesheets. A file named in at least one call during a build is written to the output folder under its fingerprinted name only; the copy under the original name, and any copy with an older fingerprint, is removed. Static files that are never given to static-url are copied under their own names as usual. Each file is hashed once per build, however many pages refer to it.

Only the bytes of the file itself count. A stylesheet’s fingerprint does not change when a file it refers to with @import or url() changes, and such references are not rewritten: they must name files that are not themselves fingerprinted.

Raises an error if the static folder has no file at path, or if called outside a build context (see current-output-dir).

To recognize copies with an older fingerprint, the build treats any file in the output folder named like "name-8 hex digits.ext" as belonging to the static file "name.ext", when the static folder has one at the same relative path.

Added in version 1.1 of package camp-lib.

9.3.7 Additional Output Files🔗ℹ

parameter

(current-output-dir) → (or/c path? #f)

(current-output-dir dir) → void?
  dir : (or/c path? #f)
A parameter containing the complete path of the site’s output folder. Set by build! and collect/call-with-page for the duration of the render; #f otherwise.

Render functions can use this to write files that accompany a page, such as generated images. A build leaves such files in place, with three exceptions: ".html" files that the build did not itself produce are deleted at the end of each build, as are files that look like an outdated copy of a static file (see static-url), and a clean build empties the whole output folder first. Files that are expensive to produce are best kept in a cache folder elsewhere and copied into place when missing:

(define-runtime-path card-cache "card-cache")
 
(define (ensure-card! doc ctx)
  (define name (~a (context-slug ctx) ".png"))
  (define cached (build-path card-cache name))
  (define published (build-path (current-output-dir) "cards" name))
  (unless (file-exists? cached)
    (make-parent-directory* cached)
    (draw-card! doc cached))
  (unless (file-exists? published)
    (make-parent-directory* published)
    (copy-file cached published)))

Changed in version 1.1 of package camp-lib: A build also deletes files that look like an outdated copy of a static file.

9.3.8 Date Formatting🔗ℹ

procedure

(~t t pattern) → string?

  t : date-provider?
  pattern : string?
Formats a date using CLDR (Unicode Common Locale Data Repository) patterns. This function is re-exported from the gregor library.

Common pattern elements:

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

Patterns can be combined with literal text:
(~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

(~d pattern v) → string?

  pattern : string?
  v : (or/c string? date-provider?)
Formats a date using CLDR patterns, automatically converting date strings. This is the recommended function for formatting dates from page metadata, since Punct documents provide dates as strings. The pattern comes first, matching Racket’s format convention.

;; 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
(~d "h:mm a" "2025-01-15T09:30")  ; "9:30 AM"

If v is already a date-provider?, it is passed directly to ~t. If v is a string, it is first parsed as a moment in the same way as date metadata (see Dates and time zones).