ui.util.augment_attrs

Apply default values to the attrs item of the dictionary.

This function is used internally inside ui.util.attrs. Following two examples are approximately the same.

{%- call ui.util.call(show_example) -%} {%- raw %} {%- set kwargs = {} -%} {%- set default_attrs = {"class": "hello"} -%} {{ ui.util.attrs(kwargs, default_attrs) }} {%- endraw %} {%- endcall %} {%- call ui.util.call(show_example) -%} {%- raw %} {%- set kwargs = {} -%} {%- set default_attrs = {"class": "hello"} -%} {%- set extended_kwargs = ui.util.augment_attrs(kwargs, default_attrs)-%} {{ ui.util.attrs(extended_kwargs) }} {%- endraw %} {%- endcall %}

This function can be called when macro needs to set default attributes before delegating call to a different macro.

{%- call ui.util.call(show_example) -%} {%- raw %} {%- macro button_with_id(content, id) -%} {%- set updated_kwargs = ui.util.augment_attrs(kwargs, {"id": id}) -%} {{ ui.button(content, **updated_kwargs) }} {%- endmacro %} {{ button_with_id("Click", id="test-id") }} {%- endraw %} {%- endcall %}

By default it updates attrs item in the dictionary, but you can change this behavior by providing key argument. If it's non-empty string, corresponding item will be updated. If it's a falsy value, the top-level will be updated

{%- call ui.util.call(show_example) -%} {%- raw %} {%- set data = {} -%} {%- do ui.util.augment_attrs(data, {"x": 1}) -%} {%- do ui.util.augment_attrs(data, {"y": 2}, key="aria") -%} {%- do ui.util.augment_attrs(data, {"z": 3}, key=none) -%} {{ data | tojson }} {%- endraw %} {%- endcall %}

Only missing items will be applied. If target item already contains value for the one of default keys, it will remain unchanged.

{%- call ui.util.call(show_example) -%} {%- raw %} {%- set data = {"attrs": {"a": 42}} -%} {%- do ui.util.augment_attrs(data, {"a": 1, "b": 2}) -%} {{ data | tojson }} {%- endraw %} {%- endcall %}