nurugger07 / calliope

An elixir haml parser
Apache License 2.0
198 stars 37 forks source link

Evaluate conditions in attributes #65

Open Qqwy opened 8 years ago

Qqwy commented 8 years ago

Calliope does not seem to support advanced attribute syntax like this:

.field{class: Game.finished?(game) && "finished"}

Calliope will not evaluate any code inside attribute tags, and attempt to add above-mentioned code as a string to the class attribute. When attempting to use string-interpolation instead, Calliope will attempt to read it as AST and choke on it with other errors.

This would be a great feature to have. I have no idea how to add conditional classes to my elements without it.

It is described under 'Attributes' in the original Haml reference guide

smpallen99 commented 8 years ago

Use string interpolation and it should work fine

Sent from my iPhone

On May 25, 2016, at 2:22 PM, Qqwy notifications@github.com wrote:

Calliope does not seem to support advanced attribute syntax like this:

.field{class: Game.finished?(game) && "finished"}

Calliope will not evaluate any code inside attribute tags, and attempt to add above-mentioned code as a string to the class attribute. When attempting to use string-interpolation instead, Calliope will attempt to read it as AST and choke on it with other errors.

This would be a great feature to have. I have no idea how to add conditional classes to my elements without it.

It is described under 'Attributes' in the original Haml reference guide

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub

Qqwy commented 8 years ago

Unfortunately, this is not the case.

If I have the following HAML:

.two.fields.discount_related{class: "#{Subscription.has_discount_period?(@changeset.model) && "disabled"}"}
   Some text

Calliope generates the following compile-time error:

(CalliopeException) web/templates/dashboard/subscription/form.html.haml: Invalid attribute 'disabled"' on line number 52`
    lib/calliope/parser.ex:180: Calliope.Parser.raise_error/3
    lib/calliope/parser.ex:137: Calliope.Parser.merge_attributes/2
    lib/calliope/parser.ex:37: Calliope.Parser.parse_line/2
    lib/calliope/parser.ex:22: Calliope.Parser.parse_lines/1
    lib/calliope/parser.ex:22: Calliope.Parser.parse_lines/1
    lib/calliope/parser.ex:18: Calliope.Parser.parse/1
    lib/calliope/render.ex:7: Calliope.Render.precompile/1
udfalkso commented 7 years ago

+1 for this. Would be very handy. I hit the same invalid attribute error.

@Qqwy A workaround that I've found is this:

- discount_class = Subscription.has_discount_period?(@changeset.model) && "disabled"
.two.fields.discount_related{class: "#{discount_class}"}
   Some text