Closed merriman-xyz closed 4 years ago
In your section
tag function, attributes
will contain a list of keys/values. In this case it will be '((id "TITLE"))
. You can use Racket’s assoc
function to grab the things you want out of there. So, for example (first (assoc 'id attributes))
. An even easier way would be to use the attr-ref
from the txexpr
package. (attr-ref attributes 'id)
does the same thing as the assoc
example above.
#lang racket
(require pollen/tag ; for define-tag-function
txexpr) ; for attr-ref
;; ensure the tag function is available to Pollen source files
(provide (all-defined-out))
(define-tag-function (section attrs elems)
(define title (string-titlecase (attr-ref attrs 'id)))
`(section ,attrs (h2 ,title) ,@elems))
Thanks @otherjoel for the lucid explanation.
Installed txepr
via raco pkg install txexpr
and was able to extract the value of the id
attribute with ease.
Lovely being able to build components out of a single command.
Have been playing with Pollen for a bit and after the basics have been trying more advanced features.
Currently I am trying to create a section class that creates a
<section>
tag with an inner<h2>
tag that uses the text of the parent section's id.In other words, take the following Pollen markup:
and output the following HTML:
My current attempts have been via
define-tag-function
. I have been able to nest theh2
tag, but am having trouble accessing the attributes list and grabbing the id for the innerh2
tag there. Ideally I could grad the ID and transform it into title case.Any help would be appreciated!