14 Module reference
14.1 Cache
14.2 Core
14.3 Decode
14.4 File
14.5 Pagetree
14.6 Render
14.7 Setup
14.8 Tag
14.9 Template
14.10 Top
On this page:
default-tag-function
define-tag-function
6.3.90.900

14.8 Tag

 (require pollen/tag) package: pollen

Convenience functions for working with tags.

procedure

(default-tag-function id    
  kw-attr-name    
  kw-attr-value ...    
  ...)  (-> txexpr?)
  id : txexpr-tag?
  kw-attr-name : keyword?
  kw-attr-value : string?
Make a default tag function for id. The new tag function takes an optional set of X-expression attributes (txexpr-attrs?) followed by X-expression elements (txexpr-elements?). From these, the tag function creates a tagged X-expression using id as the tag.

Examples:
> (require pollen/tag)
> (define beaucoup (default-tag-function 'em))
> (beaucoup "Bonjour")

'(em "Bonjour")

> (beaucoup '((id "greeting")) "Bonjour")

'(em ((id "greeting")) "Bonjour")

Entering attributes this way can be cumbersome. So for convenience, the new tag function provides an alternative: any keyword arguments and their values will be interpreted as attributes.

Examples:
> (require pollen/tag)
> (define beaucoup (default-tag-function 'em))
> (beaucoup #:id "greeting" #:class "large" "Bonjour")

'(em ((class "large") (id "greeting")) "Bonjour")

You can also provide keyword arguments to default-tag-function itself, and they will become default attributes for every use of the tag function.

Examples:
> (require pollen/tag)
> (define beaucoup-small (default-tag-function 'em #:class "small"))
> (beaucoup-small #:id "greeting" "Bonjour")

'(em ((class "small") (id "greeting")) "Bonjour")

Pollen also uses this function to provide the default behavior for undefined tags. See #%top.

Note that while default tag functions are typically used to generate tagged X-expressions, they don’t enforce any restrictions on input, so they also do not guarantee that you will in fact get a valid tagged X-expression as output. This is intentional — default tag functions are a coding convenience, and their output is likely to be processed by other tag functions, so raising the error here would be premature.

Examples:
> (require pollen/tag)
> (define strange (default-tag-function 'div #:class "bizarre"))
; Invalid data types for elements
> (strange + *)

'(div ((class "bizarre")) #<procedure:+> #<procedure:*>)

; Double "class" attribute
> (strange #:class "spooky")

'(div ((class "bizarre") (class "spooky")))

syntax

(define-tag-function
(tag-id attr-id elem-id) body ...)
Helper function for making custom tag functions. Handles parsing chores, including conversion of keyword arguments into attributes (described in default-tag-function), and parses other attributes and elements normally.

Examples:
> (require pollen/tag)
> (define-tag-function (tag-name attrs elems)
    `(new-name ,(cons '(zim "zam") attrs) ,@elems))
> (tag-name "Hello world")

'(new-name ((zim "zam")) "Hello world")

> (tag-name '((key "value")) "Hello world")

'(new-name ((zim "zam") (key "value")) "Hello world")

> (tag-name #:key "value" "Hello world")

'(new-name ((zim "zam") (key "value")) "Hello world")