How to perform conditional transformations

261 views
Skip to first unread message

rrc7cz

unread,
Nov 17, 2009, 4:41:56 AM11/17/09
to Enlive
Hey guys, I'm having a bit of trouble getting conditional
transformations to work using the deftemplate macro. Here is my code:

(deftemplate login-page "login.html" [title failure]
[:title] (prepend title)
[:form] (set-attr :action "/login" :method "post")
[:#login_wrapper :> :div] (if (not failure) (substitute nil)))

Basically the login template by default has the "Login failure"
message so that a designer can manage it. There are times when I would
NOT like to render this div, like when the login page is first
rendered. As such, I created this "failure" parameter which I'd like
to act as a flag for allowing the error message to render or not.

The problem is that (substitute) always gets called, no matter what
value failure is.

Christophe Grand

unread,
Nov 17, 2009, 8:45:13 AM11/17/09
to enliv...@googlegroups.com
Hi,
The problem is that when failure is true (if (not failure) (substitute
nil))) evaluates to nil and when failure is false it evaluates to
(substitute nil).
Both nil and (substitute nil) have the same consequences: they prune
the current node.
The correct right-hand value for this rule is #(when failure %). When
failure return the current node else remove it (return nil).

(deftemplate login-page "login.html" [title failure]
[:title] (prepend title)
[:form] (set-attr :action "/login" :method "post")
[:#login_wrapper :> :div] #(when failure %))

hth,

Christophe

--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

Robert Campbell

unread,
Nov 17, 2009, 9:27:48 AM11/17/09
to enliv...@googlegroups.com
Thanks Christophe, that makes sense. It also happens to work :-)
> --
>
> You received this message because you are subscribed to the Google Groups "Enlive" group.
> To post to this group, send email to enliv...@googlegroups.com.
> To unsubscribe from this group, send email to enlive-clj+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/enlive-clj?hl=.
>
>
>
Reply all
Reply to author
Forward
0 new messages