<p>The title field supports ternary syntax conditionals. You could do that like this:</p>

<p><code>{firstName} {middleName|length ? middleName ~ ' '}{lastName}</code></p>

<p>That says "if middleName has length, print it out, and append a space with <code>~</code>.</p>

<p>Also part of the ternary syntax is the "else" statement, which looks like the below. This is the code I use for most of my asset alt texts for accessibility.</p>

<p><code>{{ asset.assetAltText|length ? asset.assetAltText : asset.title }}</code></p>

<p>If asset.assetAltText has a length, use that. Otherwise use the asset title as the alt text</p>

CC BY-SA 4.0
2019-10-30T16:09:20.513Z
Excellent, thank you.
CreateSean
CC BY-SA 4.0
2019-10-30T15:58:39.417Z
2019-10-30T16:04:06.683Z
2019-10-30T16:04:06.683Z
darylknight
<p>Can you use the <a href="https://twig.symfony.com/doc/2.x/filters/join.html" rel="nofollow noreferrer">join</a> filter in these fields? If so, something like this may be a little cleaner:</p>

<pre><code>{{ [object.firstName, object.middleName, object.lastName] | join(' ') }}
</code></pre>

CC BY-SA 4.0
2019-11-01T20:16:14.707Z
2019-11-01T20:16:14.707Z
Trevor Davis
<p>I've got a new channel for Authors where there are 3 fields:</p>

<p>firstName
middleName
lastName</p>

<p>and I'm using these fields to generate the Title of the channel entry. However I would like to add a conditional around middleName so that it isn't used if empty and I can avoid have double space between first and last name.</p>

<p>How can I do this?</p>

<p><a href="https://i.sstatic.net/NHMO8.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/NHMO8.jpg" alt="enter image description here"></a></p>

<conditional><channels><titles>
Using Conditionals in Title Format
# Using Conditionals in Title Format

(Asked by: username_0 on 2019-10-30)

I've got a new channel for Authors where there are 3 fields:

firstName
middleName
lastName

and I'm using these fields to generate the Title of the channel entry. However I would like to add a conditional around middleName so that it isn't used if empty and I can avoid have double space between first and last name.

How can I do this?

[![enter image description here](https://i.sstatic.net/NHMO8.jpg)](https://i.sstatic.net/NHMO8.jpg)

**Tags:** conditional, channels, titles


---

## 2 answers

### Answer by username_1 on 2019-10-30. Score: 2 | Accepted answer

The title field supports ternary syntax conditionals. You could do that like this:

`{firstName} {middleName|length ? middleName ~ ' '}{lastName}`

That says "if middleName has length, print it out, and append a space with `~`.

Also part of the ternary syntax is the "else" statement, which looks like the below. This is the code I use for most of my asset alt texts for accessibility.

`{{ asset.assetAltText|length ? asset.assetAltText : asset.title }}`

If asset.assetAltText has a length, use that. Otherwise use the asset title as the alt text

> **Comments:**
> **username_0**: Excellent, thank you.  

---

### Answer by username_2 on 2019-11-01. Score: 1

Can you use the join filter in these fields? If so, something like this may be a little cleaner:
{{ [object.firstName, object.middleName, object.lastName] | join(' ') }}


---

