Inserting X-expressions into templates #48

Closed
opened 9 years ago by basus · 2 comments
basus commented 9 years ago (Migrated from github.com)

I'm trying to build a blog-like system on top of Pollen. I've been putting properties like creation and modification date into the 'meta. At a point in the template I have code which looks like:

<p>Originally published: ◊(mk-date ◊(select-from-metas 'published metas))</p>

The mk-date function is defined in my directory-require.rkt as:

(define (mk-date date-string)
  (date-display-format 'american)
  (make-txexpr 'date '()
               (list (date->string (string->date date-string "~Y-~m-~d")))))

However, it looks like the output of mk-date is not processed as an X-expression. What I see in the HTML is: <p>Originally published: pSunday, April 19th, 2015</p>.

Admittedly, the mk-date function is a bit overkill for this particular application, but I'll probably want to do similar things, where some user-specified fields in 'metas are processed into more complicated structures before getting injected into a template.

I'm trying to build a blog-like system on top of Pollen. I've been putting properties like creation and modification date into the `'meta`. At a point in the template I have code which looks like: ``` <p>Originally published: ◊(mk-date ◊(select-from-metas 'published metas))</p> ``` The `mk-date` function is defined in my `directory-require.rkt` as: ``` (define (mk-date date-string) (date-display-format 'american) (make-txexpr 'date '() (list (date->string (string->date date-string "~Y-~m-~d"))))) ``` However, it looks like the output of `mk-date` is not processed as an X-expression. What I see in the HTML is: `<p>Originally published: pSunday, April 19th, 2015</p>`. Admittedly, the `mk-date` function is a bit overkill for this particular application, but I'll probably want to do similar things, where some user-specified fields in `'metas` are processed into more complicated structures before getting injected into a template.
basus commented 9 years ago (Migrated from github.com)

I just realized that changing the template line to be

<p>Originally published: ◊(->html ◊(mk-date ◊(select-from-metas 'published metas)))</p>

makes things work fine. Should I consider this is the default/best way to handle this kind of thing.

I just realized that changing the template line to be ``` <p>Originally published: ◊(->html ◊(mk-date ◊(select-from-metas 'published metas)))</p> ``` makes things work fine. Should I consider this is the default/best way to handle this kind of thing.
mbutterick commented 9 years ago (Migrated from github.com)

Right. Anytime you want to put an X-expression into a template (HTML or otherwise), you need to explicitly specify the conversion function. For HTML templates, usually ->html is the solution. This is true of the main doc export, but also any other X-expressions you add to the template.

Your solution will work, though the HTML will end up looking like this:

<p>Originally published: <date>Saturday, May 30th, 2015</date></p>

The ->html function has a #:splice option that will discard the surrounding tag, so this:

<p>Originally published: (->html #:splice #t (mk-date (select-from-metas 'published metas)))</p>

Gets you this:

<p>Originally published: Saturday, May 30th, 2015</p>

For date and time manipulations, I recommend gregor.

Right. Anytime you want to put an X-expression into a template (HTML or otherwise), you need to explicitly specify the conversion function. For HTML templates, usually [`->html`](http://pkg-build.racket-lang.org/doc/pollen/Template.html?q=->html#%28def._%28%28lib._pollen%2Ftemplate..rkt%29._-~3ehtml%29%29) is the solution. This is true of [the main `doc` export](http://pkg-build.racket-lang.org/doc/pollen/second-tutorial.html?q=-%3Ehtml#%28part._tutorial-2._.The_-_html_function_and_the_doc_variable%29), but also any other X-expressions you add to the template. Your solution will work, though the HTML will end up looking like this: ``` html <p>Originally published: <date>Saturday, May 30th, 2015</date></p> ``` The `->html` function has a `#:splice` option that will discard the surrounding tag, so this: ``` racket <p>Originally published: ◊(->html #:splice #t ◊(mk-date ◊(select-from-metas 'published metas)))</p> ``` Gets you this: ``` html <p>Originally published: Saturday, May 30th, 2015</p> ``` For date and time manipulations, I recommend [`gregor`](https://github.com/97jaz/gregor).
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen#48
Loading…
There is no content yet.