cgrand/enlive

Conditional macro

CaptainLexington opened this issue · 4 comments

First I'd like to say I love enlive and I wouldn't use anything else for templating; however, I find I am increasingly in need of conditional application of transformations. Initially it was just making a select option as selected, which could be done with a conditional inside the transformation. This time, though, I need to either apply one transformation or, if the test fails, and entirely separate transformation.

I'm afraid I don't know the feasibility of this, but I'd really like to be able to some kind of if-transform macro that allows for this robustly. I've only seen very, very simple kinds of behavior expressed conditionally on the web. If there is in fact a more robust way to do it already, I'd love to know.

Thanks!

To be sure we are on the same like, could you provide me with one
simple example and the pseudocode you'd like to write instead?

Thanks,

Le vendredi 15 août 2014, CaptainLexington notifications@github.com a
écrit :

First I'd like to say I love enlive and I wouldn't use anything else for
templating; however, I find I am increasingly in need of conditional
application of transformations. Initially it was just making a select
option as selected, which could be done with a conditional inside the
transformation. This time, though, I need to either apply one
transformation or, if the test fails, and entirely separate transformation.

I'm afraid I don't know the feasibility of this, but I'd really like to be
able to some kind of if-transform macro that allows for this robustly. I've
only seen very, very simple kinds of behavior expressed conditionally on
the web. If there is in fact a more robust way to do it already, I'd love
to know.

Thanks!


Reply to this email directly or view it on GitHub
#113.

On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting & Contracting http://lambdanext.eu/

Absolutely!

Let's say I have a hand of cards. It might looks like this:
{:player "Bob" :cards [AC 3H 6S 8D KC]}

So normally I'd have a snippet like this:

[:p.cards]
(clone-for [card (:cards hand)]
                (content card))

Works for most situations! But sometimes there's a name for a certain arrangement of cards. I might have a hand that looks like this:
{:player "Alice" :cards [2C 2H 2D 5C 5S] :name "Full house"}

I'd want the final HTML to display "Full house" instead of the cards selected. Of course, this is where my example falls apart: there's almost no application where you would want to see only the hand's name and not the specific cards. But in my own more complicated example, you would only ever display one or the other.

So what I would like to be able to do is something along the lines of this:

 [:p.cards]
 (if (:name hand)  ; should be any arbitrary expr
     (content (:name hand))
     (clone-for [card (:cards hand)]
          (content card)))

Does this example make sense?

Thanks for your quick response!

I must be missing something since your pseudocode looks a lot like actual
working code.

Le vendredi 15 août 2014, CaptainLexington notifications@github.com a
écrit :

Absolutely!

Let's say I have a hand of cards. It might looks like this:
{:player "Bob" :cards [AC 3H 6S 8D KC]}

So normally I'd have a snippet like this:

[:p.cards](clone-for [card %28:cards hand%29]
%28content card%29)

Works for most situations! But sometimes there's a name for a certain
arrangement of cards. I might have a hand that looks like this:
{:player "Alice" :cards [2C 2H 2D 5C 5S] :name "Full house"}

I'd want the final HTML to display "Full house" instead of the cards
selected. Of course, this is where my example falls apart: there's almost
no application where you would want to see only the hand's name and not the
specific cards. But in my own more complicated example, I assure you you
would only ever display one or the other.

So what I would like to be able to do is something along the lines of this:

[:p.cards](if %28:name hand%29 ; should be any arbitrary expr
%28content %28:name hand%29%29
%28clone-for [card %28:cards hand%29]
%28content card%29%29)

Does this example make sense?

Thanks for your quick response!


Reply to this email directly or view it on GitHub
#113 (comment).

On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting & Contracting http://lambdanext.eu/

Hmmm. It seems you are correct! A conditional I had tried earlier was failing for reasons I did not fully understand, and it turns out it was unrelated. My apologies. Thanks!