Setting an attribute dynamically

50 views
Skip to first unread message

Sven Richter

unread,
May 14, 2014, 4:57:13 PM5/14/14
to enliv...@googlegroups.com
Hi,

I want to set an attribute dynamically. This was my naive implementation:
(html/defsnippet role-option (str globals/template-path "admin.html") [(html/attr= :field "role_option")]
                 [option selected]
                 [(html/attr= :field "role_option")] (html/do->
                                                       (html/content (name option))
                                                       (when selected (html/set-attr :selected selected))))

This has the problem that "when" returns nil if it does not hit the condition and then do-> will try to do something with "nil" which does not work and lead to a NPE.
What is the enlive way to achieve this? Are there any code examples? I have not found anything that makes sense to me.

Best Regards,
Sven

xavi

unread,
May 14, 2014, 5:17:29 PM5/14/14
to enliv...@googlegroups.com
Instead of

    (when selected (html/set-attr :selected selected))

you can use

    (if selected (html/set-attr :selected selected) identity)

I this should work.

Xavi

Sven Richter

unread,
May 14, 2014, 5:40:51 PM5/14/14
to enliv...@googlegroups.com
Thank you very much, that did the trick :-)
Reply all
Reply to author
Forward
0 new messages