polarblau / meta_wordpress

Use your favorite meta languages such as Haml, Sass and Coffeescript to bootstrap and build Wordpress themes.
MIT License
9 stars 1 forks source link

HTML attributes without value break when using interpolation #17

Open polarblau opened 11 years ago

polarblau commented 11 years ago

In HTML(5) the following are equal and all mark the element as checked.

<input type="radio" name="foo" checked="checked" />
<input type="radio" name="foo" checked="" />
<input type="radio" name="foo" checked />

Haml allows to set a boolean as value which will cause the attribute to show up or not.

%input(input type="radio" name="foo" checked=false)
-# <input type="radio" name="foo" />

%input(input type="radio" name="foo" checked=true)
-# <input type="radio" name="foo" checked />

However, this isn’t possible when a PHP statement defines if the input is supposed to be checked as the HTML will naturally already be compiled when the PHP is executed.

-# The following will cause the input to be always marked as checked
-# as an empty string is also considered as TRUE
= php "$checked = $a == $b ? 'checked' : ''"
%input(input type="radio" name="foo" checked="#{ php("echo $checked") }")

-# The Haml parser does not allow for attributes without value
= php "$checked = $a == $b ? 'checked=\"checked\"' : ''"
%input(input type="radio" name="foo" #{ php("echo $checked") })
-# Error: Invalid attribute list: "(input type=\"radio\" name=\"foo\" \#{ php(\"echo $checked\") })".

To get this to work at all atm., one has to resolve to pure HTML which is plain nasty:

= php "$checked = $a == $b ? 'checked' : ''"
= php "echo '<input type=\"radio\" name=\"foo\" ' . $checked . ' />'"