* SXML and optional attributes
@ 2024-12-22 21:23 Luis Felipe
2024-12-22 21:38 ` Maxime Devos via General Guile related discussions
0 siblings, 1 reply; 3+ messages in thread
From: Luis Felipe @ 2024-12-22 21:23 UTC (permalink / raw)
To: Guile User
[-- Attachment #1.1.1: Type: text/plain, Size: 1126 bytes --]
Hi,
I'd like to add attributes to SXML elements conditionally, but I don't
see how.
I'm looking for a hypothetical "if*" that allows me to do the following:
(define* (special-input #:key name (required? #true))
"Return an SXHTML INPUT element for typing text."
`(input
(@ (type "text")
(name ,name)
,(if* required? '(required "")))))
The built-in "if" doesn't work here because it will return
#<unspecified> when the condition is false, which is not an acceptable
attribute. I'm not aware of any value that can be returned in an "else"
expression that would mean "nothing" to SXML; the empty list is not it:
,(if required? '(required "") '())
I'm hoping to find something better than the following workaround:
(define* (special-input #:key name (required? #true))
"Return an SXHTML INPUT element for typing text."
(if required?
`(input (@ (type "text") (name ,name) (required "")))
`(input (@ (type "text") (name ,name)))))
Any ideas?
--
Luis Felipe López Acevedo
https://luis-felipe.gitlab.io/
[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 2881 bytes --]
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: SXML and optional attributes
2024-12-22 21:23 SXML and optional attributes Luis Felipe
@ 2024-12-22 21:38 ` Maxime Devos via General Guile related discussions
2024-12-23 0:01 ` Luis Felipe
0 siblings, 1 reply; 3+ messages in thread
From: Maxime Devos via General Guile related discussions @ 2024-12-22 21:38 UTC (permalink / raw)
To: Luis Felipe, Guile User
Unquote-splicing (,@) is your friend:
`(foo ,@(if p? '(a (b) c) '()) d)
When p? -> (foo a (b) c d).
When (not p?) -> (foo d).
Now do this for SXML …
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: SXML and optional attributes
2024-12-22 21:38 ` Maxime Devos via General Guile related discussions
@ 2024-12-23 0:01 ` Luis Felipe
0 siblings, 0 replies; 3+ messages in thread
From: Luis Felipe @ 2024-12-23 0:01 UTC (permalink / raw)
To: Maxime Devos, Guile User
[-- Attachment #1.1.1: Type: text/plain, Size: 823 bytes --]
On 22/12/24 21:38, Maxime Devos wrote:
>
> Unquote-splicing (,@) is your friend:
>
> `(foo ,@(if p? '(a (b) c) '()) d)
>
> When p? -> (foo a (b) c d).
>
> When (not p?) -> (foo d).
>
> Now do this for SXML …
>
Ah, splicing... yes Maxime, that works :)
So I applied it as follows:
(define* (special-input #:key name (required? #true))
"Return an SXHTML INPUT element for typing text."
`(input
(@ (type "text")
(name ,name)
,@(if required? '((required "")) '()))))
And now converting to XML I get the results I expected:
(sxml->xml (special-input #:name "author"))
→ <input type="text" name="author" required="" />
(sxml->xml (special-input #:name "author" #:required? #false))
→ <input type="text" name="author" />
Thank you very much,
[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 2881 bytes --]
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-12-23 0:01 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-22 21:23 SXML and optional attributes Luis Felipe
2024-12-22 21:38 ` Maxime Devos via General Guile related discussions
2024-12-23 0:01 ` Luis Felipe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).