> Nota Poin wrote on 02/11/2016 04:48 AM:
>> But there's no way I've ever been able to figure to produce generated text 
>> that is just a flat list of strings, if I ever want to have conditional 
>> decisions about what to go in that list.

I'm supposing you know that Racket has at least two native representations 
(X-expression and SXML) for HTML-like structures, so hacking HTML tags by hand 
is unnecessary.

But if you must, there's a #lang for that:

Here's the literal translation of your code in `scribble/text`, which 
automatically splices all `list`s it finds: 

#lang scribble/text

@(define prev "foo")
@(define next "bar")

@(if prev
     @(if next
          @list{<p id="navigation">
            <a href="@|prev|">Prev</a>
            <a href="@|next|">next</a>
            </p>}
          @list{<p id="navigation">
            <a href="@|prev|">Prev</a>
            </p>})
     @(if next
          @list{<p id="navigation">
            <a href="@|next|">next</a>
            </p>}
          @list{<p id="navigation">
            </p>}))


A more compact refactoring of same:

#lang scribble/text

@(define prev "foo")
@(define next "bar")

<p id="navigation">
@(if prev @list{<a href="@|prev|">Prev</a>} "")
@(if next @list{<a href="@|next|">next</a>} "")
</p>


And here's the refactored version in `pollen/pre`, which is based on 
`scribble/text`. It does not splice `list`s by default, but it has a splicing 
operator `@`, and also a `when/splice` that lets you do single-branch 
conditionals.

#lang pollen/pre

◊(define prev "foo")
◊(define next "bar")

<p id="navigation">
◊when/splice[prev]{<a href="◊|prev|">Prev</a>}
◊when/splice[next]{<a href="◊|next|">next</a>}
</p>


Here's an HTML-free variant in `pollen/markup` that produces an equivalent 
X-expression (which can be easily converted to HTML):

#lang pollen/markup

◊(define prev "foo")
◊(define next "bar")

◊p[#:id "navigation"]{
◊when/splice[prev]{◊a[#:href ◊prev]{Prev}}
◊when/splice[next]{◊a[#:href ◊next]{next}}}


Finally, we can throw in a macro for fun:

#lang pollen/markup

◊(define prev "foo")
◊(define next "bar")

◊(define-syntax-rule (cond-nav id text ...)
   ◊when/splice[id]{◊a[#:href ◊id text ...]})

◊p[#:id "navigation"]{
◊cond-nav[prev]{Prev}
◊cond-nav[next]{next}}

Enjoy.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to