`when/splice' does not work on template
#132
Closed
opened 8 years ago by leafac
·
8 comments
Loading…
Reference in New Issue
There is no content yet.
Delete Branch '%!s(<nil>)'
Deleting a branch is permanent. It CANNOT be undone. Continue?
USE CASE
I want the
<title>
of the pages on my website to always contain the website name (e.g.,ACME
) and, if the particular page defines ameta
calledtitle
(e.g.,foo
), include it too.PRE-CONDITIONS
template.html.p
index.html.pm
foo.html.pm
CURRENT BEHAVIOR
Visiting
localhost:8080/index.html
shows theACME
title, as expected. Visitinglocalhost:8080/foo.html
, on the other hand, fails with:Note that the
title
is ill-formed. It is(title ("Foo" " · ") "ACME")
instead of the(title (@ "Foo" " · ") "ACME")
I expected from usingwhen/splice
.EXPECTED BEHAVIOR
when/splice
should generate a list of the form(@ …)
, which would result in(title (@ "Foo" " · ") "ACME")
, equivalent to(title "Foo" " · " "ACME")
and the page would work.WORKAROUNDS
BAD: On
template.html.p
, replace(title ◊when/splice[the-title]{◊the-title · } "ACME")
with:This fixes the case in which
title
is set (e.g.,foo.html.pm
), but breaks the case where it’s not (e.g.,index.html.pm
). The following error happens:This is due to the inconsistent return type of
when/splice
(see https://github.com/mbutterick/pollen/pull/131).BETTER: Add the following line to
template.html.p
:Now both pages with and without the
meta
calledtitle
work.I’m not sure why this works. Also, I thought that the default behavior for undefined functions would pick up
@
as an undefined function and do what the above definition for@
does, but this doesn’t happen and I don’t know the reason either.If there’s anything I can do to help, please let me know. I look forward to contributing back to Pollen, which is a life-changing piece of technology for me 😄
This problem stems from an earlier effort (commit
3f69df3ff5
) to create onewhen/splice
that applied to both X-expressions and text contexts, instead of a separatewhen/splice/text
that would be required for text.I think you've convinced me this effort has failed. This template illustrates the problem:
For this to work properly, the first
when/splice
needs to expand into an X-expression; the secondwhen/splice
needs to expand into a plain string. But there’s no way forwhen/splice
to know about its context.So, I agree it doesn't work quite right, but I'm also not yet convinced that continuing to patch
when/splice
is the right answer. Maybe it's wiser to replace it with something better.PS In the meantime, you can work around the problem by using
when/splice
in a textual context rather than within an X-expression, e.g. —PPS.
The
@
function is deliberately re-defined during a render to make it compatible with rendering into a textual context. So this fix works (within X-expressions) because you're restoring the original meaning of@
.I understand the situation better now, thank you for the careful and detailed explanation.
I’m in for the idea of having two flavors of
when/splice
. One for X-expression context (when/splice/xexpr
) and one for text contexts (when/splice/text
). Their return types would be consistent:when/splice/xexpr
would return lists andwhen/splice/text
would return strings (see https://github.com/mbutterick/pollen/pull/131 for how to get half-way there). That would require two flavors of splicing (@
), too. Something like@/xexpr
and@/text
, maybe.Of course, it would be up to you to decide which of
when/splice/xexpr
andwhen/splice/text
is more relevant and should be promoted towhen/splice
.I’ve pushed an update that I believe fixes
when/splice
(I overcame my earlier pessimism by trying harder). It changeswhen/splice
to return a list (thereby also addressing #131) and amends Pollen’s lower-level text-output routine to cooperate with the splice tag (by ignoring it). I’ve tried your original example and it now works correctly. If you find other problems you can reopen this issue.Clarification: the output routine will already splice a sublist of strings, so
"foo" ("bar") "zam"
becomes"foo" "bar" "zam"
. The change is if the sublist has a leading splice character, like"foo" (@ "bar") "zam"
, then the splice character will be ignored, so it will just become"foo" "bar" "zam"
again.I can confirm the fix is working for all my use cases. As always, thank you very much for Pollen ❤️
🤘