Skip to content

Instantly share code, notes, and snippets.

@bartwttewaall
Last active March 4, 2022 16:08
Show Gist options
  • Save bartwttewaall/8b98ecde3fe0256cb4cc5ed8d7a087ae to your computer and use it in GitHub Desktop.
Save bartwttewaall/8b98ecde3fe0256cb4cc5ed8d7a087ae to your computer and use it in GitHub Desktop.
{% set address = { firstName: 'Stack', lastName: 'Overflow', address: 'here', zipCode: '1234' } %}
{% set addressLines = address|keys|reduce((carry, key) => carry|merge({ (key): attribute(address, key) }), []) %}
{# results in: [ 'firstName' => 'Stack', 'lastName' => 'Overflow', 'address'=> 'here', 'zipCode' => '1234' ] #}
{% set allowedKeys = ['firstName', 'lastName', 'address'] %}
{% for key, line in addressLines|filter((v, k) => k in allowedKeys) %}
{{ line }}<br/>
{% endfor %}
{% macro writeHtmlArguments(options) %}
{% set parts = options|keys|reduce((carry, key) => carry|merge({ (key): attribute(options, key) }), []) %}
{% for key, value in parts %}
{{ key|kebab ~ '=' ~ value }}
{% endfor %}
{% endmacro %}
{# example usage #}
{% set options = {
class: "theme-dark",
dataHijacking: "off",
dataAnimation: "scaleDown"
} %}
<body {% block bodyAttributes %}{{ Utils.writeHtmlArguments(options) }}{% endblock %}>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment