Last active
May 2, 2018 19:57
-
-
Save olets/e093f6301af381b19153b05d9db9002c to your computer and use it in GitHub Desktop.
flip-name: go from LAST, FIRST to FIRST LAST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
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