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?
> (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.
> (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.
> (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.
> (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 ...)
> (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")