Defining a whitespace in a variable twig

I have a situation where I need to determine if there is an empty space in a variable in a branch template and handle it a little differently.

The specified names in this application are stored with middle initials in the specified name field; they are not saved separately.

What I'm trying to achieve is this: usually most of the given names are one word for example. "John" or "Susan" or something like that. In some of these cases, we have cases where the middle starting file is stored inside this variable (e.g. John J)

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

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

      

So it will look like "Smith, John, ed." This is usually fine, however in some situations where there is a middle initial it appears as "Smith, John J. Ed." - this is not true. I cannot add a period after the name as "Smith, John., Ed" would be the wrong citation standard.

What I am trying to do is detect if it contains a given_name

space followed by a single letter and then formats it differently.

Something like that:

{% 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 a regex on a twig, or is there another method?

Thank you in advance

+3


source to share


4 answers


We can translate your requirement:

has a space preceding one 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 insert this logic into a function or macro.

You can try in this working example

+2


source


It is possible, but does not necessarily handle edge cases well. Assuming you need to do this all in a branch, you can 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 a middle initial with a period, so you can print out the full name, for example:

{{ surname }}, {{first_name ~ middle_initial}}, ed

      

+1


source


You can use a regular expression with matches

the comparison operator
and add a period if it given_name

matches the pattern.

{{ given_name }}{% if given_name matches '/^.+ .$/' %}.{% endif %}

      

+1


source


Sorry I didn't give you a good answer initially. I have updated my code and my twigfiddle. It's a little simpler than @ Matteo's:

{% set given_name = 'John J' %}
{% set nameArray = given_name|split(' ') %}

{% if nameArray[1] is defined %}
    {{ nameArray[0] }} {{ nameArray[1] }}., ed.
{% else %}
    {{ nameArray[0] }}, ed.
{% endif %}

      

Where {{ nameArray[0] }}

indicates the first element of the array. If the nameArray 1 contains anything, then it is defined.

Here is a working twigfiddle to show that it works.

0


source







All Articles