Skip to content

Instantly share code, notes, and snippets.

@olets
Last active May 2, 2018 19:57
Show Gist options
  • Save olets/e093f6301af381b19153b05d9db9002c to your computer and use it in GitHub Desktop.
Save olets/e093f6301af381b19153b05d9db9002c to your computer and use it in GitHub Desktop.
flip-name: go from LAST, FIRST to FIRST LAST
{#
A twig snippet to change
LASTNAME[, OPTIONALSUFFIX], FIRSTNAME
into
FIRSTNAME LASTNAME[, OPTIONALSUFFIX]
`include` with whitespace removed
e.g. {%- include 'flip-name.html' -%}
optionally include `with {item: YOURTARGET}` and/or `with {separator: YOURSEPARATOR}`
(separator defaults to ", ")
#}
{% set separator = separator|default(', ') %}
{% set names = item.title|split(separator) %}
{% set names_count = names|length %}
{% set first_name = names[names_count - 1] %}
{% set rest = names[0] %}
{% for i in 1..(names_count - 2) if names_count > 2 %}
{% set rest = rest ~ separator ~ names[i] %}
{% endfor %}
{{first_name}} {{rest}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment