Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda