calliope icon indicating copy to clipboard operation
calliope copied to clipboard

Remove nil values from hash attributes

Open marnen opened this issue 6 years ago • 1 comments

In the original Ruby Haml implementation, when hash attributes are nil, they're removed from the hash. That is,

- null_var = nil
#test{string: "a string", null: nil, null_var: null_var}

becomes

<div id="test" string="a string"></div>

(see https://codepen.io/marnen/pen/eMgNRN/). null and null_var are both omitted.

Calliope doesn't do this: instead, the same Haml renders to

<div id="test" string="a string" null='' null_var=''></div>

so that there is an extraneous attribute.

I was hoping to be able to make a pull request to add this behavior to Calliope, but it seems like it will be quite difficult: it looks like the reason that Calliope doesn't remove null values is that it simply translates Haml to EEx without doing any variable evaluation, and EEx has no corresponding feature to Haml's hash attributes (other than name="<%= value %>", which doesn't really do the trick in this case).

I think this points to an inconvenient fact: if Calliope is going to achieve feature parity with Haml, it probably needs to render directly to Elixir (AST, perhaps), rather than simply acting as an EEx preprocessor (just as Ruby Haml doesn't render to ERb). But that's a longer-term solution. In the shorter term, what would be a good way of achieving null attribute removal in Calliope? Or should we just bite the bullet and get rid of the EEx intermediate layer?

marnen avatar Mar 19 '18 18:03 marnen

I'm starting to work on an EEx-free Haml implementation at https://github.com/marnen/hamlex. I'd be happy to merge that into Calliope if there's interest (and if I manage to implement anything :) ).

marnen avatar Mar 20 '18 18:03 marnen