Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Identifying white space in a twig variable

I have a situation where I need to identify if there is a blank space in a variable in a twig template, and to treat it slightly differently.

Given names in this application are stored with middle initials in the given name field; they are not stored separately.

What I'm trying to achieve is this: Typically most given names are one word, e.g. "John" or "Susan" or something similar. In some of these cases we have cases where the middle initial is stored within this variable (e.g. John J)

Following standard citation standards, we need to be able to list items like this:

{{ surname }}, {{ given_name }}, ed.

This way it would appear like "Smith, John, ed." This is normally fine, however in some situations, where there is a middle initial, it appears as "Smith, John J, ed." - this is incorrect. I can't add a period after the given name, as "Smith, John., ed" would be an incorrect citation standard.

What I'm trying to do is to identify if a given_name contains a space, followed by a single letter, and then format it differently.

Something like this:

{% if given_name [has a blank space preceding a single letter] %}
{{ given_name }}., ed.
{% else %}
{{ given_name }}, ed.
{% endif %}

Is there a way to do this with regex within twig, or is there another method?

Thanks in advance

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

We can translate your requirement:

has a blank space preceding a single letter

In this little algorithm:

{% set given_name = 'John J' %}

{% set given_name_elems = given_name|split(' ') %}

{% set size = given_name_elems|length %}


{% if size >0 and given_name_elems[size-1]|length == 1%}
   { given_name }}., ed.
{%else%}
   {{ given_name }}, ed.
{%endif%}

I suggest you to incapsulate this logic in a function or in a macro.

You could made some try in this working example

about 4 years ago · Santiago Trujillo Report

0

This is possible, but is not necessarily going to handle edge cases well. Assuming that you need to do the whole thing in twig, you could do something like this:

{% set nameArray = given_name | split(' ') %}
{% set first_name = nameArray[0] %}
{% set middle_initial = nameArray[1] is defined ? " "~nameArray[1]~"." : ""%}

At this point `middle_initial is now set to either an empty string or the middle initial with the period so you can output the full name like:

{{ surname }}, {{first_name ~ middle_initial}}, ed
about 4 years ago · Santiago Trujillo Report

0

You could use a regex with the matches comparison operator and add the dot if given_name matches the pattern.

{{ given_name }}{% if given_name matches '/^.+ .$/' %}.{% endif %}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!