Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda