Skip to content

Instantly share code, notes, and snippets.

@bartwttewaall
Created May 25, 2021 09:52
Show Gist options
  • Save bartwttewaall/1674dfc0712dde79324ac6a5c7fa27cc to your computer and use it in GitHub Desktop.
Save bartwttewaall/1674dfc0712dde79324ac6a5c7fa27cc to your computer and use it in GitHub Desktop.
Creates an object with keys from an array and its translated values, then outputs it as a json string
{% set labels = ['welcome', 'ok', 'cancel'] %}
{% set obj = labels|reduce((carry, key) => carry|merge({ (key): key|t|ucfirst }), {}) %}
{{ obj|json_encode }}
{# as a macro #}
{% macro translateToJSON(labels) %}{% spaceless %}
{{ labels|reduce((carry, key) => carry|merge({ (key): key|t|ucfirst }), {})|json_encode }}
{% endspaceless %}{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment