faml
faml copied to clipboard
Skip falsey attribute rendering only if it's boolean attribute
This is an optimization idea of Hamlit.
It seems there's no standard on how to render true/false/nil of non-boolean attributes.
- Haml, Slim, Faml (before this PR)
-
%span{disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil}
renders<span disabled foo></span>
. - All falsey attributes are not rendered.
- It doesn't consider boolean attributes or not.
-
- Rails
-
tag("span", disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil)
renders<span disabled="disabled" foo="true" bar="false" />
. - Falsey attributes are not rendered if it is a boolean attribute.
-
nil
attribute is not rendered if it isn't boolean attribute.
-
- Faml (after this PR), Hamlt (maybe)
-
%span{disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil}
renders<span bar='false' baz='' disabled foo='true'></span>
. - Falsey attributes are not rendered if it is a boolean attribute.
- All attributes are rendered if it isn't a boolean attribute.
-
This optimization only contributes to the performacne of "dynamic attributes" by removing runtime conditional branching.
Input
- url = 'https://wanko.cc'
%a{href: url}
Before
_buf = []; extend ::Faml::Helpers; url = 'https://wanko.cc';
; _buf << ("<a".freeze); _faml_html1 = (url); case (_faml_html1); when true; _buf << (" href".freeze); when false, nil; else; _buf << (" href='".freeze); _buf << (::Temple::Utils.escape_html((_faml_html1))); _buf << ("'".freeze); end; _buf << ("></a>\n".freeze);
; _buf = _buf.join
After
_buf = []; extend ::Faml::Helpers; url = 'https://wanko.cc';
; _buf << ("<a href='".freeze); _buf << (::Temple::Utils.escape_html((url))); _buf << ("'></a>\n".freeze);
; _buf = _buf.join