1

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

2

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

  • Excellent, thank you. – CreateSean Oct 30 at 16:09
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(' ') }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.